php正则链接
1. php怎么用正则判断文章中是否有超链接
$pos = strpos($text,'</a>');
if ($pos === false) {
echo "恭喜,没有超链接";
}else{
echo "包含超链接";
}
比正则表达式快。正则表达式一般用来解析内容才用。
2. PHP正则链接带有&符号就无效
$a='v.swf&topBar=1&id=81144212&autoplay=false&from=page';
if(preg_match('|id\=(?<id>[^&]+)|i',$a,$match)){
var_mp($match['id']);
}
3. php正则表达式提取链接
$preg='/<a .*?href="(.*?)".*?>/is';
$str ='<a href="链接">123</a><a href="链接" target="_blank">345</a><a target="_blank" href="链接">678</a>';
preg_match_all($preg,$str,$match);
var_mp($match);
4. php 正则 <a ></a> 中的 href 和img 地址
针对给定的标本数据,编写如下正则供参考:
1、提取 jpg 地址链接
"(http://.*?.jpg)"
2、提取 a href 或是 img src 标签中的 jpg 地址链接
[href|src]="(http://.*?.jpg)"
3、提取<a></a>标签中的 jpg 地址链接
(<a.*)(http://.*.jpg)(".*")(http:/.*.jpg)(".*)</a>
5. php正则获取href的链接
使用正则中的子模式,按给出的代码匹配的话大概是这样
$pattern='/href="([^(}>)]+)"/';
然后使用preg_match或者preg_match_all如果替换的话使用preg_replace即可
6. php 取超链接的正则表达式
这段代码应该是你想要的.
==========
<?php
$test =<<<EOF
<a class="t4" href="read.php?id=9576" title="ffff">********</a>
@@@@@@@@@@@@@@
<a class="t5" href="read.php?id=9576" title="ffff">@@@@@@@</a>
EOF;
$pattern = '/href="(.*?)"/';
preg_match_all($pattern,$test,$result);
//你要的结果在数组的第二个元素内.
//print_r($result);
foreach($result[1] as $ritem){
echo $ritem;
}
?>
7. php匹配html中这几段链接的正则表达式
<?php
$s=<<<STR
"index.html?s=/Home/Article/content/id/20"
"index.html?s=/Home/Article/content/id/21"
"index.html?s=/Home/Article/content/id/22"
"index.html?s=/Home/Article/content/id/999"
STR;
preg_match_all('~"(.*d+)"~',$s,$m);
print_r($m[1]);
8. php正则表达式去除超链接。
preg_replace正则匹配,去除所有a链接地址,并且保留里面a里面的内容
preg_replace(“#<a[^>]*>(.*?)</a>#is”, “$1”,$body);
ereg_replace正则匹配:
ereg_replace(“]*>|</a>”,””,$content);
ereg_replace函数匹配以”<a “开头,中间除>以外的所有字符,再以>结尾的字符串或匹配””字符。匹配到的字符串赋为空。
9. php高手请进:正则提取超链接中的网址和标题,如果兼顾有双引号和单引号或没有引号的超链接
<?php
$text = "递归是一种函数调用自身的机制。这是一种强大的特性可以把某些复杂的东西变得很简单。<a href='http://mp3..com'>MP1</a><a href=http://mp3..com>MP2</a><a href='http://mp3..com' target='_blank'>MP3</a><a href=http://mp3..com target=mainFrame>MP4</a><a href=http://mp3..com style=\"font-size:32px;color:#e53333;\">MP5</a><a href=\"http://mp3..com\">MP6</a>";
preg_match_all('/<a href=(.*)>(.*)<\/a>/isU',$text,$data_arr);
foreach( $data_arr[1] as $key=>$val ) {
$replace_str = $data_arr[0][$key];
$title = $data_arr[2][$key];
preg_match("/(https?|ftp|mms):\/\/([A-z0-9]+[_\-]?[A-z0-9]+\.)*[A-z0-9]+\-?[A-z0-9]+\.[A-z]{2,}(\/.*)*\/?/",$val,$url_data);
$url = $url_data[0];
$text = str_replace($replace_str,"<br/>\n{$title} {$url}",$text);
}
var_mp($text);
?>
这个可以识别得了网址中包含http开头的链接地址格式的。但如果还需匹配相对地址,建议是将所有可能出现的情况一一替换掉
10. PHP 正则表达式 获取字符串 所有链接
你这个也会报错 菜鸡