Regular Expressions: preg_match_all case problem
| From: | Waldo L. Jaquith | Date: | Mon, 14 Aug 2000 17:59:04 +0000 |
| Subject: | Regular Expressions: preg_match_all case problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-11647@lists.php.net to get a copy of this message | ||
Folks,
I'm hung up on a problem. I'm trying to extract a series of A tags,
complete with the content that it's wrapped around, from a string. The only
way to do this, that I can find, is using the
preg_match_all function <http://php.net/manual/function.preg-match-all.php>,
which permits multiple matches in a single string via the Perl regex
functions.
I'm using the below, slightly modified from the manual. (I changed it to
[Aa] to only get HREF tags:
preg_match_all ("/(<([Aa]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);
The problem is that it chokes on multiple links with the same "A" case. I
can have a single link that ends in "/A". And I can have another one that
ends in "/a". But if I have two "/a" or "/A" tags, it gives link text
of:
click here</A> to read the story of a <A HREF=moose.html>moose</A>
Instead of:
click here
moose
You see? I'm explaining this poorly -- sorry. RegEx makes my head hurt. :)
I would think that the \\2 backreference would be sufficient to recognize
the A or a that was picked up in the first part, but I don't know.
Could anybody offer any suggestions? I'm a little baffled. Thanks, folks.
Best,
Waldo
Waldo L. Jaquith
< waldo@munkandphyber.com >
< 804/979.2980 >
// Munk & Phyber
// 110B Second Street NE
// Charlottesville, VA 22902