如果锚标签中有字符串则匹配
Match if string is there in anchor tag
我想测试锚标签之间是否有字符串,例如:
这是示例文本 <a href=""> this is test string </a>
,这是其他锚标记 <a href=""> link again </a>
谢谢。
在上面的字符串中,如果 "test" 位于锚标记之间,我想匹配。我怎样才能用正则表达式做到这一点。
请帮忙!
谢谢。
这是一个你可以使用的函数:
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 ".
试试下面的代码:
$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";}
我想测试锚标签之间是否有字符串,例如:
这是示例文本 <a href=""> this is test string </a>
,这是其他锚标记 <a href=""> link again </a>
谢谢。
在上面的字符串中,如果 "test" 位于锚标记之间,我想匹配。我怎样才能用正则表达式做到这一点。
请帮忙!
谢谢。
这是一个你可以使用的函数:
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 ".
试试下面的代码:
$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";}