如何通过PHP从内容中找到一个URL? [英] How to find a URL from a content by PHP?

查看:116
本文介绍了如何通过PHP从内容中找到一个URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一个简单的preg_match,它会在内容中找到c.aspx(没有引号),如果它发现,它将返回整个url。作为示例

need a simply preg_match, which will find "c.aspx" (without quotes) in the content if it finds, it will return the whole url. As a example

$content = '<div>[4]<a href="/m/c.aspx?mt=01_9310ba801f1255e02e411d8a7ed53ef95235165ee4fb0226f9644d439c11039f%7c8acc31aea5ad3998&amp;n=783622212">New message</a><br/>';

现在,它应该从$ content preg_matchc.aspx,并将输出为

now it should preg_match "c.aspx" from $content and will give a output as

"/m/c.aspx?mt=01_9310ba801f1255e02e411d8a7ed53ef95235165ee4fb0226f9644d439c11039f%7c8acc31aea5ad3998&amp;n=783622212"

$内容应该有更多的链接,除了c.aspx。我不想要他们我只想要所有具有c.aspx的网址。

The $content should have more links except "c.aspx". I don't want them. I only want all url that have "c.aspx".

请让我知道我该怎么做。

Please let me know how I can do it.

推荐答案

您可以使用DOM来解析HTML,而不是正则表达式。您可以使用正则表达式来解析属性值。

You use DOM to parse HTML, not regex. You can use regex to parse the attribute value though.

编辑:更新的示例,以便检查c.aspx。

updated example so it checks for c.aspx.



$content = '<div>[4]<a href="/m/c.aspx?mt=01_9310ba801f1255e02e411d8a7ed53ef95235165ee4fb0226f9644d439c11039f%7c8acc31aea5ad3998&amp;n=783622212">New message</a>

<a href="#bar">foo</a>

<br/>';

$dom = new DOMDocument();
$dom->loadHTML($content);

$anchors = $dom->getElementsByTagName('a');

if ( count($anchors->length) > 0 ) {
    foreach ( $anchors as $anchor ) {
        if ( $anchor->hasAttribute('href') ) {
            $link = $anchor->getAttribute('href');
            if ( strpos( $link, 'c.aspx') ) {
                echo $link;
            }
        }
    }
}

这篇关于如何通过PHP从内容中找到一个URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆