Home > Software design >  How to retrieve any existing Bible verse including accentuated bible books from any content into an
How to retrieve any existing Bible verse including accentuated bible books from any content into an

Time:09-16

I have successfully been able to retrieve any bible verse that is place between brackets like (John 3:16) but I have been looking for a way in php to retrieve any bible verse in any string into an array per bible verse. None of the search result I have found on stack overflow matches what I want. Let's say I have a content like this :

Here is a content where Genèse 11:10-12, 15-17, 19-20. And other verses scattered anywhere in the content like 2 Chronicles 12:10-12, 15-17, 19-20 and 3 John 3 and soemthing else.

How can I retrieve any of these Bible verses into an array so that this array will contain the following e respective of the Genèse accent è ?

array("Genèse 11:10-12, 15-17, 19-20", "2 Chronicles 12:10-12, 15-17, 19-20", "3 John 3");

How to retrieve any existing Bible verse including accentuated bible books from any content into an array ?

CodePudding user response:

We can use preg_match_all with an appropriate regex pattern:

$input = "Here is a content where Genèse 11:10-12, 15-17, 19-20. And other verses scattered anywhere in the content like 2 Chronicles 12:10-12, 15-17, 19-20 and 3 John 3 and soemthing else.";
preg_match_all("/(?:\d  )?[A-Z]\S  \d (?::\d (?:-\d )?(?:, \d (?:-\d )?)*)?/", $input, $matches);
print_r($matches[0]);

This prints:

Array
(
    [0] => Genèse 11:10-12, 15-17, 19-20
    [1] => 2 Chronicles 12:10-12, 15-17, 19-20
    [2] => 3 John 3
)

Here is an explanation of the regex pattern:

  • (?:\d )? match optional book number (e.g. 1 Kings, 2 Chronicles)
  • [A-Z]\S match book name (includes possible accented characters)
  • space
  • \d match chapter number
  • (?: start optional group
  • : match colon
  • \d (?:-\d )? match verse number followed by optional range
  • (?:, \d (?:-\d )?)* match zero or more verse ranges
  • )? close optional group
  •  Tags:  
  • php
  • Related