0

Can anyone help me how to split /explode verse format to 3 parts?

The condition should be as follows.

the second and 3rd part must be seperated by a colon. so for example in Gen 1:1

it must be seperated as

[0] = Gen
[1] = 1
[2] = 1

it is delimited by a space (or many space to make it user friendly) and by a colon

secondly, in verses with numeric in front is a problem because user can type it as

1 Cor 1:1 and they can also type it as 1Cor 1:1

in either case I want [1] and [2] to be split by a colon and the remaining parts will be trimmed all space and became the [0] value.

is this possible? Because I am thinking of using only one text box for searching of verse and php will validate it accordingly before passing it as query.

Thanks

Wayne
  • 763
  • 4
  • 21
  • 43
  • * See also [Open source RegexBuddy alternatives](http://stackoverflow.com/questions/89718/is-there) and [Online regex testing](http://stackoverflow.com/questions/32282/regex-testing) for some helpful tools, or [RegExp.info](http://regular-expressions.info/) for a nicer tutorial. – mario Jun 13 '12 at 00:22

4 Answers4

1

I came up with the following pattern that seems to work. Try this:

<?php

$string = '7 Genesis 16:23';

if (preg_match('/^(.*?)\s+(\d+):(\d+)$/', $string, $match)) {
    echo "You selected book {$match[1]}, chapter {$match[2]}, verse {$match[3]}!";
} else {
    echo "I couldn't understand your input.  Please use 'Book chapter:verse' format.";
}

The first part of the expression (.*?) matches the book, (1 Cor) or just (Cor), so you may need/want to do additional validation on that part or refine it, but this worked okay for me. Hopefully gets you started.

drew010
  • 68,777
  • 11
  • 134
  • 162
0

Try something like this:

$str = 'Gen 1:1'; // OR: $str = '1 Cor 1:1';
$array = explode( ' ', $str);
$last = end( $array);
$array = array_merge( array_slice( $array, 0, count( $array) - 1), explode( ':', $last));
var_dump( $array);

Demo

nickb
  • 59,313
  • 13
  • 108
  • 143
  • This will fail if `$str = '1 Cor 1:1'`, one of the OP's examples – Matt Dodge Jun 13 '12 at 00:33
  • @mattedgod - Missed that, fixed it now, works for both cases, thanks. – nickb Jun 13 '12 at 00:36
  • @nickb This becomes 4 parts now. It should only be 3 parts. 1Cor or 1 Cor should end up in 1 array. The parts must be book, chapter, verse. I use **drew010** code. Works for my purpose. Thanks to all who contribute. I checked all your code and I warded the question to drew010 for being the first correct answer. Thanks all – Wayne Jun 13 '12 at 10:03
0

This pattern should work:

$subject = <<verse here>>;
$pattern = '/^([0-9a-zA-Z ]+) ([0-9]+):([0-9]+)/';
preg_match($pattern, $subject, $matches);
print_r($matches);

Note that the first element in $matches will be the full $subject. Elements 1-3 will map to your elements 0-2.

Thomas Fussell
  • 458
  • 3
  • 9
  • This also works. array[0] being the whole entry and array [1-3] getting the proper value of Book, Chapter, verse needed for my code. – Wayne Jun 13 '12 at 10:07
0

So it sounds like you want the following, in order:

  • Some text (maybe spaces maybe not) (.*)
  • Some spaces (at least 1) [ ]+
  • A number (1+ digits) ([0-9]+)
  • A colon :
  • Another number (1+ digits) ([0-9]+)

Therefore, your regex would look like so:

/^(.*)[ ]*([0-9])+:([0-9]+)$/

After running this:

$m = '1 Cor 3:14';
$matches = array();
preg_match("/^(.*)[ ]*([0-9])+:([0-9]+)$/", $m, $matches); 

$matches will look like this:

Array
(
    [0] => 1 Cor 3:14
    [1] => 1 Cor 
    [2] => 3
    [3] => 14
)
Matt Dodge
  • 10,833
  • 7
  • 38
  • 58