您的位置:首页 > 编程语言 > PHP开发

php中的正则表达式的使用

2014-05-31 14:16 369 查看
正则表达式:

preg_match()和preg_match_all()

preg_quote()

preg_split()

preg_grep()

preg_replace()
替换HTML源码中的地址

$form_html = preg_replace ( '/(?<=\saction=\" \ssrc=\" \shref=\")(?!http: javascript)(.*?)(?=\"\s)/e', 'add_url(\$url, \'\\1\')', $form_html
);

带断言的正则匹配

$match
= '';

$str = 'xxxxxx.com.cn bold font paragraph
text ';

preg_match_all ( '/(?<=<(\w{1})>).*(?=<\/\1>)/', $str, $match );

echo "匹配没有属性的HTML标签中的内容:";

print_r ( $match );

在正则中使用回调函数

/** *
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 . '/' . $match;

}

匹配action属性

$str
= '';

$match = '';

preg_match_all('/\s+action=\"(?!http:)(.*?)\"\s/', $str, $match);

print_r($match);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: