PHP String Manipulation: Extract hrefs

后端 未结 4 1911
臣服心动
臣服心动 2020-11-29 13:22

I have a string of HTML that I would like to check to see if there are any links inside of it and, if so, extract them and put them in an array. I can do this in jQuery with

4条回答
  •  日久生厌
    2020-11-29 13:42

    You can use PHPs DOMDocument library to parse XML and/or HTML. Something like the following should do the trick, to get the href attribute from a string of HTML.

    $html = '

    Doctors

    C - G G - K K - M'; $hrefs = array(); $dom = new DOMDocument(); $dom->loadHTML($html); $tags = $dom->getElementsByTagName('a'); foreach ($tags as $tag) { $hrefs[] = $tag->getAttribute('href'); }

提交回复
热议问题