How to find a URL from a content by PHP?

后端 未结 2 850
孤独总比滥情好
孤独总比滥情好 2021-01-23 09:28

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 = \'
相关标签:
2条回答
  • 2021-01-23 10:11

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

    Edit: 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;
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-23 10:24

    If you want to find any quoted string with c.aspx in it:

    /"[^"]*c\.aspx[^"]*"|'[^']*c\.aspx[^']*'/
    

    But really, for parsing most HTML you'd be better off with some sort of DOM parser so that you can be sure what you're matching is really an href.

    0 讨论(0)
提交回复
热议问题