Page 1 of 1
get strings from string
Posted: Tue Jul 20, 2010 5:05 am
by pedroz
$string="google=12 yahoo=publish bing='ok' ask=0";
how get the following from the $string?...
echo $google; output 12
echo $yahoo; output plublish
echo $bing; output 'ok'
echo $ask; output 0
Re: get strings from string
Posted: Tue Jul 20, 2010 7:04 am
by buckit
This works:
Code: Select all
<?php
$string="google=12 yahoo=publish bing='ok' ask=0";
$string=explode(' ',$string);
foreach($string as $v){
$exp = explode('=',$v);
$resultArray[$exp[0]] = $exp[1];
}
echo $resultArray['google'];
echo $resultArray['yahoo'];
echo $resultArray['bing'];
echo $resultArray['ask'];
?>
First we explode $string into an array using a space as the separator... that then puts 'google=12' as a value in the array. next we loop through that array and explode each value using = as the separator. then we put the first value as the key in a new array and the second value as the new arrays value for that key.