Home > php教程 > php手册 > body text

php匹配字符中链接地址程序代码

WBOY
Release: 2016-06-13 11:33:02
Original
1190 people have browsed it

 判断一个字符串是否含有超级链接

 代码如下  

$str="ssdsfsdfsdfss";
if(preg_match("/]*>|/[^a]*a[^>]*>/i",$str))
{
echo "该字符串有超链接";
}
else
{
echo "该字符串没有超链接标记";
}
?>


下面我们只要过滤连接部份。

 代码如下  

echo preg_replace("/(?]*)(?=>)/i","#", "你好,点这里看看你好,点这里看看");
?>

正则:/(?]*)(?=>)/

(? (?=exp) 匹配exp前面的位置
此正则 匹配 在 href= 之后 “>” 之前 的 非 “>” 的所有字符

例子:

找到这些字符(url)用 # 替换,就可以去掉html里的所有链接。

现在分享一个提取超级连接的实例

 

 代码如下  

function match_links($document) {   

    preg_match_all("']+))[^>]*>?(.*?)'isx",$document,$links);                       

    while(list($key,$val) = each($links[2])) {

        if(!empty($val))

            $match['link'][] = $val;

    }

    while(list($key,$val) = each($links[3])) {

        if(!empty($val))

            $match['link'][] = $val;

    }       

    while(list($key,$val) = each($links[4])) {

        if(!empty($val))

            $match['content'][] = $val;

    }

    while(list($key,$val) = each($links[0])) {

        if(!empty($val))

            $match['all'][] = $val;

    }               

    return $match;

}

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!