【发布时间】:2016-01-08 10:32:47
【问题描述】:
我想测试锚标签之间是否有字符串,例如:
这是示例文本<a href=""> this is test string </a>,这是其他锚标记<a href=""> link again </a> 谢谢。
在上面的字符串中,如果锚标签之间有“test”,我想匹配。如何用正则表达式做到这一点。
请帮忙!
谢谢。
【问题讨论】:
我想测试锚标签之间是否有字符串,例如:
这是示例文本<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";}
【讨论】: