我試影像str_replace函式的字串(資訊框tbs7)與像一串東西與tbs7。但我希望它即使在tbs7是別的東西時也能作業。我已經定義了(infobox $)和帶有 $ 的東西,但 $ 應該是任何東西。
我怎么做?
uj5u.com熱心網友回復:
您可以將正則運算式與 preg_grep 一起使用:
$source = '(infobox tbs7)';
$target = 'something with $2';
$pattern = '/^(\(infobox )(.*)(\))$/i';
$result = preg_replace($pattern, $target, $source);
正則運算式模式將源字串“拆分”為三部分:
'(infobox ' -> $1
'anything in between' -> $2
')' -> $3
并且 $2 用于替換字串中以指示要替換的內容。
uj5u.com熱心網友回復:
嘗試更換模式\binfobox \S 與infobox隨后的替換詞:
$input = "Here is infobox tbs7 and other things.";
$output = preg_replace("/\binfobox \S /", "infobox abc", $input);
echo $output; // Here is infobox abc and other things.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/333180.html
