0
我得到了原始的正則表達式,並且我認爲我可以使這個工作,因爲它非常相似;但是,我不能。無法通過preg_replace()查找文本
此代碼正在用於模板系統,其中鏈接被javascript或HTML取代,取決於用戶的瀏覽器是否啓用了JS。
的示例串是這樣的: help{:footerlink}{::helpFooter}
「幫助」將在鏈接本身顯示的文本。 「footerLink」是應該被應用到鏈接 「helpFooter」的CSS類是應該被應用到鏈接
這裏的CSS ID是我想使用的代碼:
$string = 'help{:footerlink}{::helpFooter}';
$classIDExp = '/([a-z0-9]+)\{:([a-z0-9]+)\}\{::([a-z0-9]+)\}/i';
$display = preg_replace($idexp, "$1", $string);
$class = preg_replace($idexp, "$2", $string);
$cssID = preg_replace($idexp, "$3", $string);
$curLink = 'Display Text: ' . $display . '<br />' .
'Class: ' . $class . '<br />CSS ID: ' . $cssID;
echo $curLink;
該代碼輸出:
Display Text: help{:footerlink}{::helpFooter}
Class: help{:footerlink}{::helpFooter}
CSS ID: help{:footerlink}{::helpFooter}
適當的輸出將是:
Display Text: help
Class: footerlink
CSS ID: helpFooter
鏈接ID檢索得很好,但找不到顯示文本,css類和css ID。我試圖逃避冒號(:),但它仍然顯示相同的輸出。
任何幫助都非常感謝,因爲正如我之前說過的,我遠不是正則表達式的專家。 :(
爲什麼你使用'的preg_replace()',而不是'的preg_match()'如果你想要的是字符串的匹配部分? – Barmar
'$ classIDExp'是不一樣的'$ idexp'在你的問題或真實的代碼中是否是一個錯字? – Barmar
對不起,我現在可以使用謝謝,我還會使用preg_match()而不是preg_replace(),謝謝指出。 – SISYN