/** * replace some string by callback function * */ function callback_replace() { $url = 'http://esfang.house.sina.com.cn'; $str = ''; $str = preg_replace ( '/(?<=saction=")(?!http:)(.*?)(?="s)/e', 'search($url, \1)', $str ); echo $str; } function search($url, $match){ return $url . 'https://www.jb51.net/' . $match; }
带断言的正则匹配
$match = '';
$str = 'xxxxxx.com.cn bold font
paragraph text
';
preg_match_all ( '/(?<=<(w{1})>).*(?=</1>)/', $str, $match );
echo "匹配没有属性的HTML标签中的内容:";
print_r ( $match );
替换HTML源码中的地址
以下为引用的内容:
$form_html = preg_replace ( '/(?<=saction="|ssrc="|shref=")(?!http:|javascript)(.*?)(?="s)/e', 'add_url($url, '\1')', $form_html );
元字符