2014-10-28 39 views
0

帮我普莱舍:警告:的preg_match():未知的修饰词 'T'

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

您需要更改getTextBetweenTags调用中的参数位置。 'abc'是标签的名称,而不是要搜索的字符串,对吗?

$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')); 
+0

感谢的咨询,但在这种情况下,我的功能 'code' $内容= getTextBetweenTags( 'ABC',$页); 工作不正确,而不是标签“abc”中的内容,我在文档$页面中获得第一个标签。 – 2014-10-29 08:29:54

+0

编辑我的答案,对不起,我开始误读你的问题。你可以调用$ content = getTextBetweenTags('abc',$ page);而$ content = getTextBetweenTags($ page,'abc');是你需要的。 – 2014-10-29 09:09:51

+0

瓦伦丁,非常感谢你,但在这种情况下,我得到了 注意:未定义的偏移量:代码中的1 echo $ matches [1]; //标记名称 注意:未定义的偏移量:2代码中的行echo $ matches [2]; //文本 旧代码 的preg_match( “/ \ <(\w+)\>(*)\ <\/\w+\> /”,$标记名,$匹配); echo $ matches [1]; //标签名称 echo $ matches [2]; //文本 在新代码中: 它是 注意:未定义的偏移量:代码中的1返回$ matches [1]; 此外,显示我的'代码'NULL(我认为由于var_dump) – 2014-10-29 09:25:29

相关问题