我有一個 preg_replace:$text = preg_replace("/(\+?[\d-\(\)\s]{8,25}[0-9]?\d)/", "$1", $text);我想在這個 preg_replace 中使用兩個子字符串。它應該是這樣的:$text = preg_replace("/(\+?[\d-\(\)\s]{8,25}[0-9]?\d)/", "<span id='number' data-last=substr($1, -5)>substr($1, 0, -5)<span>****<div class='showphone'>Show</div></span></span>", $text);注意那里的兩個子串。顯然這不是一個有效的例子,但我想知道是否有可能使它起作用。任何想法如何?
1 回答

ibeautiful
TA貢獻1993條經驗 獲得超6個贊
你不能像變量一樣在雙引號中使用函數。您也不能對捕獲的正則表達式值使用函數,您應該使用preg_replace_callback. 像這樣的東西:
$text = preg_replace_callback("/(\+?[\d-\(\)\s]{8,25}[0-9]?\d)/", function($match) {
return "<span id='number' data-last=" . substr($match[1], -5) . ">" . substr($match[1], 0, -5) . "<span>****<div class='showphone'>Show</div></span></span>";
}, $text);
應該這樣做。雖然沒有示例字符串,但我無法對此進行測試。
- 1 回答
- 0 關注
- 136 瀏覽
添加回答
舉報
0/150
提交
取消