Sunday, 1 May 2016

preg_replace

$string = 'April 15, 2003';
$pattern = '/(April) (15), (2003)/i';
$replacement = 'Month: $1 Date: $2 Year: $3';
echo preg_replace($pattern, $replacement, $string);

$string = "The quick brown fox jumps over the lazy dog";
$patterns = array("/quick/", "/brown/", "/fox/");
$replacements = array(2=>"bear", 1=>"black", 0=>"slow");
ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);

$string = 'April 15, 2003';
$pattern = '/(April) (15), (2003)/i';
$replacement = 'Month: $1 Date: $2 Year: $3';
echo preg_replace($pattern, $replacement, $string);

$count = 0;
echo preg_replace(array('/\d/', '/\s/'), '*', 'xp 4 to', -1 , $count);
echo $count; //3

No comments:

Post a Comment