php - Replacing string with variables stored in an array -
ok i'm unsure syntax on one, think logic self explanatory i'm unsure if preg_replace work or whether loop needed end result.
$string = $randomizer->fecthrandomphrase($cfg['seo']['meta']['descriptions']['single'], 3, $_server['request_uri']);
returns string
lorem ipsum dolor sit amet,[address1], [address2], [postcode]. consectetur adipiscing elit. mauris id dui sem, eget laoreet tellus. vivamus lacinia vestibulum odio lobortis - [region]
i search string parts want change;
$find = array('[address1]','[address2]','[postcode]','[region]');
i pull information stored in these variables , place them in array;
$replace = array($address1,$address2,$postcode,$region);
the before returning phrase apply preg_replace swap on info have stored
$phrase = preg_replace($find,$replace,$string);
do need loop through array $replace allow reading of each variable , replace work or using wrong function entirely?
str_replace()
can take arrays first 2 parameters, might want consider instead. otherwise, you'd need form proper regex $find
in order invoke preg_replace()
once, not doing.
usage:
$phrase = str_replace( $find, $replace, $string);
now $phrase
shoud contain desired output.
Comments
Post a Comment