php - Explode string after price -


say have string

$what = "1 x book @ $20 32 x rock music cd @ $400 1 x shipping india @ $10.5"; 

and want explode output

array  (     [0] => 1 x book @ $20     [1] => 32 x rock music cd @ $400     [2] => 1 x shipping india @ $10.50 ) 

i thinking below dont know regular expressions use!

 $items = preg_split('/[$????]/', $what); 

thanks in advance

try :

$str = '1 x book @ $20 32 x rock music cd @ $400 1 x shipping india @ $10.5'; preg_match_all('/(?p<match>\d+\s+x\s+[\w\s]+\s+@\s+\$[\d\.]+)/',$str,$match);  echo "<pre>"; print_r($match['match']); 

output :

array (     [0] => 1 x book @ $20     [1] => 32 x rock music cd @ $400     [2] => 1 x shipping india @ $10.5 ) 

another solution :

as per dream eater' comment : smaller way write it: (.*?\$\d+)\s. – dream eater 3 mins ago

i added * @ end , works fine.

$str = '1 x book @ $20 32 x rock music cd @ $400 1 x shipping india @ $10.5'; preg_match_all('/(.*?\$\d+)\s*/',$str,$match);  echo "<pre>"; print_r($match); 

ref: http://php.net/manual/en/function.preg-match.php


Comments