php 正则表达式 preg_replace

阅读更多
练习一:
$1";
$replacement2 = 'strtoupper("$1")';
echo preg_replace($pattern1, $replacement1, $txt);
//This is a link to www.google.com

echo "
"; echo preg_replace($pattern2, $replacement2, $txt); //This is a link to WWW.GOOGLE.COM ?>


练习二:
$txt = "sss  &";
$pattern = '/[&<">]/e';
$replacement = array('&' => '&', '<' => '<', '>' => '>', '"' => '"');
echo preg_replace($pattern, '$replacement["$0"]', $txt);
//sss <b> &


练习三:
$subjects = array("sss  &", "aaa");
$patterns = array('/&/', '//', '/"/');
$replacements = array('&', '<', '>', '"');
$rs_array = preg_replace($patterns, $replacements, $subjects);
print_r($rs_array);

/*
Array
(
    [0] => sss <b> &
    [1] => <b>aaa</b>
)
*/


preg_replace_callback函数
" . $matches[0] . "";
}
$rs = preg_replace_callback($pattern, "fn_callback", $txt);
echo $rs;
//vim --- vim -
?>

你可能感兴趣的:(正则表达式,PHP,vim,Google)