1

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.

Sachin
  • 113
  • 10
  • Possible duplicate of [Regexp for extracting all links and anchor texts from HTML](http://stackoverflow.com/questions/4624848/regexp-for-extracting-all-links-and-anchor-texts-from-html) – maxhb Jan 08 '16 at 10:41

2 Answers2

2

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 ".
Jérémy Halin
  • 553
  • 4
  • 29
  • but string may have lot of data.. in a big string there are few anchors tags and among that anchor tags i need to find. so that case it may not work. – Sachin Jan 08 '16 at 10:38
  • thank you Jeremy for your prompt reply.. and its a good answer.. but in case I have multiple anchor tags in a string then I wanted to know. that too not full text only a word I need. – Sachin Jan 08 '16 at 10:47
  • 1
    You really wanted to match "test", sorry I though it was an example ;) – Jérémy Halin Jan 08 '16 at 10:52
1

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";}
Amit Verma
  • 40,709
  • 21
  • 93
  • 115