Wednesday, June 23, 2010

Reguler Expression for finding “href’s” value:

Lets take a string

$content = "Test <a href=\"http://www.testone.com\">Test Link</a> Tedst <a href=\"http://www.testdone.com\">Testd Link</a> ";

We will use follwing reguler expression to find href's values

$regex_pattern = "/<a href=\"(.*)\">(.*)<\/a>/";

Matching the string with reguler expression, we get it on $matches[1] place.
So complete code is... 

<?php 
$content = "Test <a href=\"http://www.testone.com\">Test Link</a> Tedst <a href=\"http://www.testdone.com\">Testd Link</a> ";
$regex_pattern = "/<a href=\"(.*)\">(.*)<\/a>/"; preg_match_all($regex_pattern,$content,$matches);


print $matches[1];


?>

No comments:

Post a Comment