php中的正则表达式的使用
2014-05-31 14:16
369 查看
正则表达式:
替换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);
preg_match()和preg_match_all() preg_quote() preg_split() preg_grep() preg_replace() |
$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);
相关文章推荐
- 在PHP中使用正则表达式
- PHP正则表达式使用详解
- 收集常用的PHP正则表达式及使用
- PHP正则表达式的几则使用技巧
- 正则表达式使用详解 (php,perl,unix,javascript) (有点意思)
- 正则表达式使用详解 (php,perl,unix,javascript)
- php中使用正则表达式
- PHP 正则表达式的几则使用技巧
- PHP正则表达式使用详解
- 收集常用的PHP正则表达式及使用
- 在PHP中使用正则表达式进行查找替换 推荐
- php中正则表达式的使用
- 在PHP中使用正则表达式
- php正则表达式的模式修正符和逆向引用使用介绍
- 在PHP中使用与Perl兼容的正则表达式
- PHP正则表达式使用详解
- php中正则表达式使用详解
- 在PHP中使用与Perl兼容的正则表达式
- 正则表达式的问题(php环境中使用)
- php正则表达式的模式修正符和逆向引用使用介绍