Jump to content

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.

Thank you all

the code is great,

I got as output:

 

Array

(

    [0] => sentence one

    [1] => sentence two

)

 

is it possible to remove the word Array ( [0] etc... so each setence go on a separate line

 

thank you all again

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.