How can I find an HTML tag in a string with PHP?
This is a record in my database:
$str = "This is me <img src='images/mardagz.png' width='200' /> :) when i was 4th year highschool hahah so funny...";
I’m trying to get the contents of the <img> tag. This is the code I am using:
$gimg = preg_match('/<img[^>]+\>/i', $str , $matches) ? $matches[1]: '<img src="http://facebook.com/username/profile.png" width="200" />
But, this code always gives me this error: Notice: Undefined offset: 1 in mardagz.blog\post.php on line 19
What should I do?
$matches[1]holds the text that matched the first group in the search pattern, but your pattern does not have any groups. Groups are defined with parentheses.Either put your whole pattern in a group like this:
or just use
$matches[0]to get the whole text that matched.