How to find a URL from a content by PHP?

后端 未结 2 881
孤独总比滥情好
孤独总比滥情好 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 = '
    [4]New message foo
    '; $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; } } } }

提交回复
热议问题