Page 1 of 1

Regex Split

Posted: Wed Aug 04, 2004 12:13 pm
by dledmonds
I'm hoping this is easy to achieve, but I'm a bit lost as to how without using a whole bunch of strpos and substr functions. I've seen regex in php, but they only seem to split/match based on one pattern.

Some text ...
"<start>blah blah<end><start>more blah<end>"

I'd like to split the text starting with <start>, ending with <end>, returning an array containing

"blah blah"
"more blah"

Thanks.

Posted: Wed Aug 04, 2004 12:17 pm
by feyd

Code: Select all

preg_split('#start_pattern(.*?)end_pattern#',$subject,$limit,PREG_SPLIT_DELIM_CAPTURE);

Posted: Wed Aug 04, 2004 12:22 pm
by hawleyjr
Another way to do it:

Code: Select all

<?php
$myStr = '<start>blah blah<end><start>more blah<end>';
$myStr = str_replace('</end>','',$myStr);

$myArray = explode('<start>',$myStr);
?>