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


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


source


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


source


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


source







All Articles