What would be the appropriate regex to delimit a string by all whitespaces? There can also be more than one whitespace between two token and it should ignore whitespaces at the end.
What I have so far:
<?php
$tags = "unread dev test1 ";
$tagsArr = preg_split("/ +/", $tags);
foreach ($tagsArr as $value) {
echo $value . ";";
}
This gives me the following output:
"unread;dev;test1;;"
As you can see it doesn't ignore the whitespaces at the end because the correct output should be:
"unread;dev;test1;"
You can ignore empty entries using the flag PREG_SPLIT_NO_EMPTY
:
$tagsArr = preg_split("/ +/", $tags, -1, PREG_SPLIT_NO_EMPTY);
Demo: http://ideone.com/1hrNJ
Just use the trim function first to cut away the white space at the end.
$trimmed_tags = trim($tags);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With