More flexible verse formatting - book names with accents

Created on 30 December 2011, over 12 years ago
Updated 27 February 2024, 4 months ago

This is a follow up to http://drupal.org/node/301678. I want the filter to work with french bible book names. Unfortunately the PHP Regular Expressions do not match characters with accents, which means it is not simply a matter of adding all the french book names to $book_namelist:

  global $book_namelist;
  $book_namelist = 'Gen|Genesis|Genèse;';
  $book_namelist .= 'Exo|Exodus|Exode;';
  $book_namelist .= 'Lev|Leviticus|LĂ©vitique;';
  $book_namelist .= 'Num|Numbers|Nom|Nombres;';
  $book_namelist .= 'Deu|Deuteronomy|Deutéronome;';
  $book_namelist .= 'Jos|Josh|Joshua|Josué;';
  $book_namelist .= 'Jug|Judges|Jug|Juges;';
  $book_namelist .= 'Rut|Ruth;';
  $book_namelist .= '1 Sam|1 Samuel;';
  $book_namelist .= '2 Sam|2 Samuel;';
  $book_namelist .= '1 Ki|1 Kings|1 Ro|1 Roi|1 Rois;';
  $book_namelist .= '2 Ki|2 Kings|2 Ro|2 Roi|2 Rois;';
  $book_namelist .= '1 Chr|1 Chro|1 Chron|1 Chronicles|1 Chroniques;';
  $book_namelist .= '2 Chr|2 Chro|2 Chron|2 Chronicles|2 Chroniques;';
  $book_namelist .= 'Ezra|Esd|Esdras;';
  $book_namelist .= 'Neh|Nehemiah|Néhémie;';
  $book_namelist .= 'Tob|Tobit|Tobie;';
  $book_namelist .= 'Jdt|Judith;';
  $book_namelist .= 'Est|Esther;';
  $book_namelist .= '1 Mc|1 Maccabees|1 Mac;';
  $book_namelist .= '2 Mc|2 Maccabees|2 Mac;';
  $book_namelist .= '3 Mc|3 Maccabees|3 Mac;';
  $book_namelist .= '4 Mc|4 Maccabees|4 Mac;';
  $book_namelist .= 'Job;';
  $book_namelist .= 'Ps|Psa|Psm|Psalm|Psalms|Psaume|Psaumes;';
  $book_namelist .= 'Psalm 151;';
  $book_namelist .= 'Pro|Prov|Proverbs|Proverbes;';
  $book_namelist .= 'Ecc|Ecclesiastes|Ecclésiaste;';
  $book_namelist .= 'Song|Song of Solomon|Can|Cantique|Cantique des cantiques|Cantique de Salomon;';
  $book_namelist .= 'Wis|Wisdom|Sagesse;';
  $book_namelist .= 'Sir|Sirach|Ecq|Siracide;';
  $book_namelist .= 'Isa|Isaiah|Isaie|Ésaïe;';
  $book_namelist .= 'Jer|Jeremiah|Jérémie;';
  $book_namelist .= 'Lam|Lamentations;';
  $book_namelist .= 'Bar|Baruch;';
  $book_namelist .= 'Eze|Ezek|Ezekiel|Ézéchiel;';
  $book_namelist .= 'Dan|Daniel;';
  $book_namelist .= 'Hos|Hosea|Osée;';
  $book_namelist .= 'Joel|Joël;';
  $book_namelist .= 'Amo|Amos;';
  $book_namelist .= 'Obad|Obadiah|Abd|Abdi|Abdias;';
  $book_namelist .= 'Jona|Jonah|Jonas;';
  $book_namelist .= 'Mic|Micah|Michée;';
  $book_namelist .= 'Nah|Nahum;';
  $book_namelist .= 'Hab|Habakkuk|Habaccuc;';
  $book_namelist .= 'Zep|Zephaniah|Sop|Sophonie;';
  $book_namelist .= 'Hag|Haggai|Agg|Aggée;';
  $book_namelist .= 'Zec|Zech|Zechariah|Zac|Zacharie;';
  $book_namelist .= 'Mal|Malachi|Malachie;';
  $book_namelist .= 'Mat|Matt|Matthew|Matthieu;';
  $book_namelist .= 'Mar|Mark|Marc;';
  $book_namelist .= 'Luke|Luc;';
  $book_namelist .= 'John|Jn|Jean;';
  $book_namelist .= 'Act|Acts|Actes;';
  $book_namelist .= 'Rom|Romans|Romains;';
  $book_namelist .= '1 Cor|1 Corinthians|1 Corinthiens;';
  $book_namelist .= '2 Cor|2 Corinthians|2 Corinthiens;';
  $book_namelist .= 'Gal|Galatians|Galates;';
  $book_namelist .= 'Eph|Ephesians|Éphésiens;';
  $book_namelist .= 'Phi|Phil|Phili|Phillippians|Philippiens;';
  $book_namelist .= 'Col|Colossians;';
  $book_namelist .= '1 Thes|1 Thessalonians|1 Thessaloniciens;';
  $book_namelist .= '2 Thes|2 Thessalonians|2 Thessaloniciens;';
  $book_namelist .= '1 Tim|1 Timothy|1 Timothée;';
  $book_namelist .= '2 Tim|2 Timothy|2 Timothée;';
  $book_namelist .= 'Tit|Titus|Tite;';
  $book_namelist .= 'Phm|Philemon|Philémon;';
  $book_namelist .= 'Heb|Hebrews|HĂ©breux;';
  $book_namelist .= 'Jam|James|Jac|Jacques;';
  $book_namelist .= '1 Pet|1 Peter|1 Pi|1 Pie|1 Pierre;';
  $book_namelist .= '2 Pet|2 Peter|2 Pi|2 Pie|2 Pierre;';
  $book_namelist .= '1 John|1 Jn|1 Jean;';
  $book_namelist .= '2 John|2 Jn|2 Jean;';
  $book_namelist .= '3 John|3 Jn|3 Jean;';
  $book_namelist .= 'Jud|Jude;';
  $book_namelist .= 'Rev|Revelation|Apo|Apocalypse;';
  $book_namelist .= '1 Esdras;';
  $book_namelist .= '2 Esdras;';
  $book_namelist .= 'Epistle of Jeremiah;';
  $book_namelist .= 'Prayer of Azariah;';
  $book_namelist .= 'Susanna;';
  $book_namelist .= 'Bel and the Dragon;';
  $book_namelist .= 'Prayer of Manasseh;';
  $book_namelist .= 'Psalm of Solomon;';
  $book_namelist .= 'Odes';

The solution I came up with (in addition to the above), is to use str_replace() to replace the accented booknames in the list with something preg_replace() would work with. Because I am using an HTML format, I also had to take care of HTML entities in the text as well! So I created 3 global arrays:

  global $accented_books;
  $accented_books = array("Genèse", "Lévitique", "Deutéronome", "Josué", "Néhémie", "Ecclésiaste", "Ésaïe", "Jérémie", "Ézéchiel", "Osée", "Joël", "Michée", "Aggée", "Éphésiens", "1 Timothée", "2 Timothée", "Philémon", "Hébreux");
  global $unaccented_books;
  $unaccented_books = array("GenEseFR", "LEvitiqueFR", "DeutEronomeFR", "JosuEFR", "NEhEmieFR", "EcclEsiasteFR", "EsaIeFR", "JErEmieFR", "EzEchielFR", "OsEeFR", "JoElFR", "MichEeFR", "AggEeFR", "EphEsiensFR", "1 TimothEeFR", "2 TimothEeFR", "PhilEmonFR", "HEbreuxFR");
  global $htmlaccented_books;
  $htmlaccented_books = array("Genèse", "Lévitique", "Deutéronome", "Josué", "Néhémie", "Ecclésiaste", "Ésaïe", "Jérémie", "Ézéchiel", "Osée", "Joël", "Michée", "Aggée", "Éphésiens", "1 Timothée", "2 Timothée", "Philémon", "Hébreux");

$unaccented_books could be anything - as long as it's not already used in the text or $book_namelist
I then updated function _bible_blsaddLink:

  global $book_list, $book_namelist, $unaccented_books, $accented_books, $htmlaccented_books;
  $book_regex = $book_list .'|'. str_replace(';', '|', str_replace($accented_books, $unaccented_books, $book_namelist));
  $pattern_regex = '/('. $book_regex .')[:. ]+(\d{1,3}):(\d{1,3}(?:-\d{1,3})?)([#\^]\w+)?(\s?)/e';
  $replacement_regex = '_bible_blsdivhtml("\1", "\2", "\3", "\4", "\5")';
  $text = preg_replace($pattern_regex, $replacement_regex, str_replace($htmlaccented_books, $unaccented_books, $text));
  return str_replace($unaccented_books, $htmlaccented_books, $text);

and function _bible_blsdivhtml:

function _bible_blsdivhtml($book, $chap, $verse, $para, $tail) {
...
    global $book_list, $book_namelist, $unaccented_books, $accented_books;
    if (preg_match('/'.$book_list.'/', $book)) {
      // For Reguler BLS, Ex: GEN:1:1
      $bls = _bible_get_chapter_sname($bid, $book). ':'. $chap. ':'. $verse;
    }
    else {
      // For other naming, Ex: Gen.1:2
      $bls = $book. ':'. $chap. ':'. $verse;
      $patt_array = explode(';', '/'. str_replace(';', '%/A;/', str_replace('|', '%|', str_replace($accented_books, $unaccented_books, $book_namelist))). '/');
      $book_array = explode('|', $book_list);
      $book = preg_replace($patt_array, $book_array, $book.'%');
    }
...

Seems to work - probably needs some more thought... but let me know if you'd like a patch...

regards

Matt

✨ Feature request
Status

Closed: outdated

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom Matt B

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024