Explode()

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
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Explode()

Post 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.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Explode()

Post by Apollo »

Code: Select all

$sentences = explode('.', str_replace(array('?','!'), '.', $input));
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Re: Explode()

Post 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) "" }
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Explode()

Post by AbraCadaver »

I would do this:

Code: Select all

$sentences = preg_split('/[.!?]/', $input, null, PREG_SPLIT_NO_EMPTY);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Re: Explode()

Post by lenton »

THANKS, that's perfect.
Post Reply