Regex Split

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dledmonds
Forum Newbie
Posts: 2
Joined: Wed Jul 21, 2004 10:40 am

Regex Split

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

preg_split('#start_pattern(.*?)end_pattern#',$subject,$limit,PREG_SPLIT_DELIM_CAPTURE);
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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);
?>
Post Reply