5 Replies - 1332 Views - Last Post: 29 March 2011 - 12:13 PM Rate Topic: -----

#1 dannzyx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-September 08

preg_match to search within html tags

Posted 27 March 2011 - 05:00 AM

Good day!

I am currently learning the whole preg_match abacadabra.
To do this I decided to write a small app. to search within html tags of a page;
However I stumbled upon a problem when trying to search within alt tags:

I only want to search within the alt tags of images.
Using this code:
preg_match_all('/(<img\s) .* alt="(\w*)" .* (>)/ismxU', $code, $result);



It does not work the way I want it to, it does not seem to search only within the image tags...

Can anyone point me in the right direction?

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: preg_match to search within html tags

#2 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 132
  • View blog
  • Posts: 674
  • Joined: 03-August 06

Re: preg_match to search within html tags

Posted 27 March 2011 - 10:15 AM

Try changing this
/(<img\s) .* alt="(\w*)" .* (>)/ismxU
To this
/(<img\\s) .* alt="(\\w*)" [^>]* (>)/ismxU

This post has been edited by grimpirate: 27 March 2011 - 10:18 AM

Was This Post Helpful? 0
  • +
  • -

#3 dannzyx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-September 08

Re: preg_match to search within html tags

Posted 27 March 2011 - 10:26 AM

Thanks for your reply, however it still only gives me the alt content outside of the img tag.
Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 934
  • View blog
  • Posts: 2,332
  • Joined: 15-February 11

Re: preg_match to search within html tags

Posted 27 March 2011 - 11:14 AM

PCRE functions are crazy stuff :sweatdrop:

But however this retrieves the alt text in IMG links. It's hard to explain but I'm using (.+?) to match all the characters within the quotes in an <img /> tag.

$str = '<a href="link.html" alt="Link">Link Here</a> <img src="image.jpg" alt="Image" />';
preg_match_all("#<img.+?alt=\"(.+?)\" />#", $str, $matches);
var_dump($matches);

Was This Post Helpful? 0
  • +
  • -

#5 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 132
  • View blog
  • Posts: 674
  • Joined: 03-August 06

Re: preg_match to search within html tags

Posted 27 March 2011 - 10:41 PM

I would suggest the following changes to codeprada's expression:
'#<img[^>]+?alt="([^>]+?)"[^>]+?>#'
The reasons are as follows:
It's easier to read if you don't have to escape the double quotations.
As I understand it a negated character class matches more efficiently than a . multi-character, and in order for the . multi-character to match newlines you have to add the 's' flag to your expression.
It's possible the img tag may not close elegantly with a space, a slash, and a greater than sign, but we do know it definitely terminates with a greater than sign.

This post has been edited by grimpirate: 27 March 2011 - 10:47 PM

Was This Post Helpful? 1
  • +
  • -

#6 dannzyx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-September 08

Re: preg_match to search within html tags

Posted 29 March 2011 - 12:13 PM

Still didnt do the job :<
Thanks for the additional explanation grim.
it's very helpfull in understanding the whole matter.

Ill let you know if I find a solution
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1