Page 1 of 1
Explode()
Posted: Sat Jul 03, 2010 6:41 pm
by lenton
I need to explode a string where there is either a '.' or '?' or '!'
I don't know how I can do this for all three characters.
This is what I tried and it didn't work:
Code: Select all
$sentences = explode("." "?" "!", $input);
Thanks for your help.
Re: Explode()
Posted: Sat Jul 03, 2010 7:03 pm
by Apollo
Code: Select all
$sentences = explode('.', str_replace(array('?','!'), '.', $input));
Re: Explode()
Posted: Sat Jul 03, 2010 7:09 pm
by lenton
Great thanks!
Only problem is if someone puts a full stop on the end of the sting it creates a blank array on the end:
example.
array(2) { [0]=> string(4) "example" [1]=> string(0) "" }
Re: Explode()
Posted: Sat Jul 03, 2010 8:10 pm
by AbraCadaver
I would do this:
Code: Select all
$sentences = preg_split('/[.!?]/', $input, null, PREG_SPLIT_NO_EMPTY);
Re: Explode()
Posted: Sun Jul 04, 2010 7:59 am
by lenton
THANKS, that's perfect.