Search code examples
phpregexstringhtml-parsingtext-parsing

Get whole img tag including its attribute declarations from a string


I have html string in variable in php. I want to get tag from it. For example :

$str ='<p><img src="link"></p><p>text</p>';
   

How can I get <img src="link"> (or any img tag plus its content) from this string?


Solution

  • User Regex in php. You should write regular expression for it

    http://php.net/manual/en/function.preg-match.php

    <img\s[^<>]*>