请帮助我:
function getTextBetweenTags($string, $tagname)
{
$pattern = "/<$tagname>(.*?)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
我得到了:
Warning: preg_match(): Unknown modifier 't'
另外,我试过:
$pattern = "~<$tagname>(.*?)<\/$tagname>~";
在这种情况下,我有:
Warning: preg_match(): Compilation failed: range out of order in character class at offset 146 in
当然,我尝试过变种
$pattern = "/<".$tagname.">(.*?)<\/".$tagname.">/";
有什么想法吗? )
答案 0 :(得分:0)
您需要更改 getTextBetweenTags 调用中的参数位置。 &#39; ABC&#39;是标签的名称,而不是要搜索的字符串,对吗?
$html = "<html><body><abc>HELLO</abc></body></html>";
function getTextBetweenTags( $string, $tagname ) {
preg_match( "/\<{$tagname}\>(.*)\<\/{$tagname}\>/", $string, $matches );
return $matches[1];
}
var_dump( getTextBetweenTags( $html, 'abc' ) );