2014-10-28 76 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

相關問題