Sachin
Sachin

Reputation: 113

Match if string is there in anchor tag

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

Answers (2)

Amit Verma
Amit Verma

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

J&#233;r&#233;my Halin
J&#233;r&#233;my Halin

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

Related Questions