How to properly explode a string
How to explode a string like
#heavy / machine gun #test
to get just "heavy"
and "test"
?
I tried to explode, but so far I only got " heavy / machine gun
" and " test
"
Thanks in advance, Jeremy.
+3
Jรฉrรฉmie Zarca
source
to share
3 answers
Explosion alternative:
$str = "#heavy / machine gun #test";
preg_match_all('/#([^\s]+)/', $str, $matches);
var_dump($matches[1]);
This will basically find all the hashtags in a given string.
+1
Wayne whitty
source
to share
Re-blast your "first" part in the spaces to get only the "heavy"
$heavy = explode (' ', $my_previous_explode[1]); // I guessed you exploded on '#', so array index = 1
0
Sugar
source
to share
The regex is obviously more robust in this case, but just for fun:
$str = '#heavy / machine gun #test';
$arr = array_filter(array_map(function($str){
return strtok($str, ' ');
}, explode('#', $str)));
& raquo; Example
0
Emissary
source
to share