Jump to content

[SOLVED] Help Brackets Splitter


ballouta

Recommended Posts

Hello

 

Kindly i am looking for to split sentences between brackets, e.g.:

 

$test ="this is sentence number one (1) here is sentence number two (2)";

 

is it possible to print them like this? how?

 

this is sentence number one

here is sentence number two

 

Thank You

Link to comment
https://forums.phpfreaks.com/topic/148527-solved-help-brackets-splitter/
Share on other sites

$test ="this is sentence number one (1) here is sentence number two (2)";
$arr = preg_split('#\(\d\) ?#', $test, -1, PREG_SPLIT_NO_EMPTY);
echo '<pre>'.print_r($arr, true);

 

Edit: If there is a chance of sentences going into (xx), then simply change \d to \d+ (in fact, no real harm in just using \d+ regardless)

Yeah.. I thought about that afterwards... I think yours is more 'bulletproof' (in that it cleanly strips out spaces on both sides of the brackets). So to the OP, my suggestion, use CV's pattern and throw in the -1, PREG_SPLIT_NO_EMPTY for extra measure.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.