search
Homephp教程php手册php匹配字符中链接地址的方法

这篇文章主要介绍了php匹配字符中链接地址的方法,涉及正则匹配与提取超链接的方法,是非常实用的正则技巧,需要的朋友可以参考下

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

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

复制代码 代码如下:

$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(!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程序设计有所帮助。

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.