Home > Backend Development > PHP Tutorial > php匹配字符中链接地址的方法_php技巧

php匹配字符中链接地址的方法_php技巧

WBOY
Release: 2016-05-16 20:27:42
Original
1039 people have browsed it

本文实例讲述了php匹配字符中链接地址的方法。分享给大家供大家参考。具体如下:

判断一个字符串是否含有超级链接,代码如下:

复制代码 代码如下:

下面我们只要过滤连接部份,代码如下:
复制代码 代码如下:
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(!emptyempty($val))
            $match['link'][] = $val;
    }
    while(list($key,$val) = each($links[3])) {
        if(!emptyempty($val))
            $match['link'][] = $val;
    }       
    while(list($key,$val) = each($links[4])) {
        if(!emptyempty($val))
            $match['content'][] = $val;
    }
    while(list($key,$val) = each($links[0])) {
        if(!emptyempty($val))
            $match['all'][] = $val;
    }               
    return $match;
}

希望本文所述对大家的PHP程序设计有所帮助。

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template