Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex, return all <a....> .... </a> from html

Status
Not open for further replies.

gbrian

IS-IT--Management
Sep 22, 2004
96
0
0
US
Hello,

I need to create a regular expression so that I can replace anything in an html source file with blank if it does not match the criteria <a *> *</a>

Example,
<html>
<strong><a href="asdf.com"> test1 </a></strong>
<strong><a href="asdf.com"> test2 </a></strong>
</html>

would return:
<a href="asdf.com"> test1 </a>
<a href="asdf.com"> test2 </a>

I think a regular expression would be the easiest way to do this (preg_replace)

Thanks in advance for your help.
 
Code:
$pattern = "/<a\b[^>]*>(.*?)</a>/";
preg_match_all($pattern, $html, $matches);
print_r($matches);

this checks for "a" and not "A". easy enough to make it case insensitive.
 
Thanks for this, for some reason it is giving me an error:

Unknown modifier 'a' in ...

Any ideas?
 
sorry, forgot the escaping

Code:
<?
$html = "This is a <a href='#'>link</a> to <a href='#'>something</a>";
$pattern = "/<a\b[^>]*>(.*?)<\/a>/";
preg_match_all($pattern, $html, $matches);
print_r($matches);
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top