php中的正则表达式的使用

来源:互联网 发布:cf卡测速软件 编辑:程序博客网 时间:2024/06/04 05:16

正则表达式:

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);

0 0
原创粉丝点击