I was trying to split a string on non-alphanumeric characters or simple put I want to split words. The approach that immediately came to my mind is to use regular expressions.
Example:
$string = 'php_php-php php';
$splitArr = preg_split('/[^a-z0-9]/i', $string);
But there are two problems that I see with this approach.
- It is not a native php function, and is totally dependent on the PCRE Library running on server.
- An equally important problem is that what if I have punctuation in a word
Example:
$string = 'U.S.A-men's-vote';
$splitArr = preg_split('/[^a-z0-9]/i', $string);
Now this will spilt the string as[{U}{S}{A}{men}{s}{vote}]
But I want it as[{U.S.A}{men's}{vote}]
So my question is that:
- How can we split them according to words?
- Is there a possibility to do it with php native function or in some other way where we are not dependent?
Regards
this sentence.and this one too.? And what aboutI am sure this regex is a no-go but I'll use it anyway.U.S.Ato be a word, you'd need a non-space-padded stop mark to not be a word separator. So you could split on whitespaces, question marks, commas, colons, and so on, OR spaced stop marks.preg_splitis not native? Please show me a PHP installation since the late 1920s that does not supportpreg_splitmysql/mysqli/PDO, because these are extensions? What aboutmb_*? You just have to be realistic at some point...