Forum Moderators: coopster

Message Too Old, No Replies

html parsing

         

scorpion

1:29 am on Mar 11, 2003 (gmt 0)

10+ Year Member



Anybody know an easy way to parse data in an html tag? I hate having to write 4 lines getting the start and end position, etc..

e.g.

alt="myname"

I want to put "myname" in a string...how to parse out the tag quickly? Assume the raw string contains the above...

Or is there a prewritten PHP html parsing function?

aaronc

2:16 am on Mar 11, 2003 (gmt 0)

10+ Year Member



You probably want to use a regular expression for that.
I'm assuming that the raw string only has the html tag in it, nothing else.
$string = "<img src='image.gif' alt='myname'>";
preg_match("/^<.* alt=['¦\"]*(\w*)['¦\"]*.*>$/", $string, $match);
$match[1] should contain the myname.
I'm no regular expression wiz and I did this rather quickly so there might be some bugs in it.