Jump to content

Exploding strings


gerkintrigg

Recommended Posts

preg_split('/\s+|[,?!]/',$string);

 

There might be another, non-preg function, but I don't know it offhand.

 

I'll second that suggestion. The only thing you'll have to watch out for is the fact that this will split all words on to separate lines. So, even if you have something like an email address that contains one of your punctuations for which you are searching, you'll split on it:

 

<?php
$string = "[email protected] is my email!";
$words = preg_split('|[\s,?!.]|', $string);
// results in array('i', 'the', 'bomb@mydomain', 'co', 'uk')
?>

 

You may be better off just doing a preg_match_all() on all word boundaries:

<?php
preg_match_all('|\b(.+?)\b|', $string, $words, PREG_PATTERN_ORDER);
?>

 

Hope this helps.

Link to comment
https://forums.phpfreaks.com/topic/49329-exploding-strings/#findComment-241742
Share on other sites

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.