這就是我試圖實現(xiàn)的想法$foo = $data['string']; $bar = trim(preg_replace($patterns, $replacements, $data['string']), "_" || "-"); echo $bar;我試圖實現(xiàn)的結(jié)果:example_示例或示例 - 到示例在某些情況下,我使用preg_replace()的場景可能會以“_”或“-”結(jié)尾。在這種情況下,我想使用trim()來刪除它。顯然,情況是將這些字符保留在字符串中:Example_example_到Example_example或示例-示例-到示例-示例
3 回答

白衣染霜花
TA貢獻(xiàn)1796條經(jīng)驗 獲得超10個贊
的值是 ,因為 PHP 邏輯運算符總是返回一個布爾值,而這兩個字符串都是真實的。第二個參數(shù) to 必須是字符串,并轉(zhuǎn)換為字符串 ,因此您正在從字符串中修剪該字符。"_" || "-"
TRUE
trim()
TRUE
"1"
的第二個參數(shù)應(yīng)只是一個字符串,其中包含要從末尾刪除的所有字符,而不是帶有 的表達(dá)式。trim()
||
$bar = trim(preg_replace($patterns, $replacements, $data['string']), "_-");

慕雪6442864
TA貢獻(xiàn)1812條經(jīng)驗 獲得超5個贊
將要修剪的字符作為第二個參數(shù)提供給 :trim()
$string = 'Example_example_';
echo trim($string, '_-'); //Example_example

桃花長相依
TA貢獻(xiàn)1860條經(jīng)驗 獲得超8個贊
試試這個正則表達(dá)式:
(-|_)(?:^|\s)
$re = '/(-|_)(?:^|\s)/m';
$str = 'The is test-test_ and Test_test- and the end';
$str = preg_replace($re,'',$str);
echo $str;
將輸出:
測試-測試并Test_testand結(jié)束
- 3 回答
- 0 關(guān)注
- 144 瀏覽
添加回答
舉報
0/150
提交
取消