Reputation: 113
I want to test if string is there between anchor tags, for example :
this is example text <a href=""> this is test string </a>
and here is other anchor tag <a href=""> link again </a>
thanks.
In above string I want to match if "test" is there between anchors tags. how can I do it with regular expression.
Kindly help !
Thanks.
Upvotes: 1
Views: 1536
Reputation: 41219
Try the following code :
$x='<a href="">This is a test string</a>';
if(preg_match_all('~<a href="">.+test.+</a>~i',$x,$m))
{echo "Match";}
else
{echo "No match";}
Upvotes: 1
Reputation: 563
Here is a function you can use:
function getTextBetweenTags($string, $tagname) {
$pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
$str = '<a href=""> this is test string </a>';
$txt = getTextBetweenTags($str, "a");
echo $txt;
// Will return " this is test string ".
Upvotes: 2