ballouta Posted March 8, 2009 Share Posted March 8, 2009 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 Quote Link to comment Share on other sites More sharing options...
.josh Posted March 8, 2009 Share Posted March 8, 2009 $test ="this is sentence number one (1) here is sentence number two (2)"; $sentences = preg_split("~\s?\(\d+\)\s?~",$test); Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted March 8, 2009 Share Posted March 8, 2009 $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) Quote Link to comment Share on other sites More sharing options...
.josh Posted March 8, 2009 Share Posted March 8, 2009 ooh good call on the no empty flag. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted March 8, 2009 Share Posted March 8, 2009 Yeah, when I first tested without out, I got an empty entry (hate those). Quote Link to comment Share on other sites More sharing options...
.josh Posted March 8, 2009 Share Posted March 8, 2009 your pattern doesn't match the space before the (..) though, so it will be added to the end of the sentence. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted March 8, 2009 Share Posted March 8, 2009 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. Quote Link to comment Share on other sites More sharing options...
ballouta Posted March 9, 2009 Author Share Posted March 9, 2009 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 Quote Link to comment Share on other sites More sharing options...
.josh Posted March 9, 2009 Share Posted March 9, 2009 dude...it's an array. the print_r was to just dump it so you can see how its structured. Use a foreach loop to loop through each element and display it how you want it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.