0

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?

ThatOtherPerson
  • 834
  • 6
  • 18
Pseudorandom
  • 716
  • 3
  • 14
  • 30
  • possible duplicate of [Grabbing the href attribute of an A element](http://stackoverflow.com/questions/3820666/grabbing-the-href-attribute-of-an-a-element) – Gordon Dec 07 '11 at 13:34
  • 3
    Use the search function before asking. Do not ask duplicates. – Gordon Dec 07 '11 at 13:35
  • 5
    @Gordon: Take it easy. The actual question is not "how to parse HTML", but "where is the bug in my code snippet". In particular, this is not a duplicate of the question you are referring to. – Ferdinand Beyer Dec 07 '11 at 14:33
  • 4
    @FerdinandBeyer fix my code questions are equally unwelcome on StackOverflow as yet another question asking how to get something from html. The duplicate shows how to fetch elements and attributes. There is hundred similar ones availabe. Pick one if the one above is not good enough for you. There is absolutely no reason why we should reward lazy OPs with an answer when they refuse to do research before asking or insist on reusing existing solutions. As a 15k+ user you should support the community and closevote instead of repwhoring on this. – Gordon Dec 07 '11 at 14:40
  • 3
    @Gordon: I find it rather offensive that you accuse me of "repwhoring" here. While I do agree that users should search before they ask, and that there are plenty of similar questions on SO, I think there is no harm in pointing newbies to obvious mistakes. We all started as newbies and -- as the FAQs read -- we are all here to learn together. – Ferdinand Beyer Dec 07 '11 at 15:07
  • @Ferdinand You can point newbies to obvious mistakes by pointing them to appropriate duplicates showing them how to solve their problems. Answering duplicates makes SO harder to search and only benefits your reputation and the OP's laziness. The harm is done to the community and the site as a whole in the long tail. – Gordon Dec 07 '11 at 15:16

2 Answers2

14

$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:

preg_match('/(<img[^>]+>)/i', $str, $matches)

or just use $matches[0] to get the whole text that matched.

Ferdinand Beyer
  • 64,979
  • 15
  • 154
  • 145
3

You could get the img tag with regex but if I were you I wouldn't use regex for this.

I think a better idea would be to use an HTML parser like this: http://simplehtmldom.sourceforge.net/

Or you could use PHP's DOM Library: http://jp.php.net/dom

Lucas
  • 10,476
  • 7
  • 39
  • 40