Get multiple substrings from a string in PHP -
i want extract multiple substrings string , put in array..
for example:
$string = '{[john][16 years old][new york]}{[michael][22 years old][las vegas]}{[smith][32 years old][chicago]}'; $array = ["john,16 years old, new york, mihael, 22 years old, las vegas, smith, 32 years old, chicago"]; someone ideea?
a simple preg_match it:
$string = '{[john][16 years old][new york]}{[michael][22 years old][las vegas]}{[smith][32 years old][chicago]}'; preg_match_all('/\[(.*?)\]/', $string , $matches, preg_pattern_order); print_r($matches[1]); /* array ( [0] => john [1] => 16 years old [2] => new york [3] => michael [4] => 22 years old [5] => las vegas [6] => smith [7] => 32 years old [8] => chicago ) */ demo:
update:
to loop matches can use:
preg_match_all('/\[(.*?)\]/', $string, $matches, preg_pattern_order); ($i = 0; $i < count($matches[1]); $i++) { echo $matches[1][$i]; }
Comments
Post a Comment