2017-10-11 70 views
1

我只想問,我想從後續字符串中刪除最後的OR。如何從字符串中刪除OR的最後一次出現

$string = 'b.dates LIKE "2017-10-13%" OR 
b.dates LIKE "2017-10-14%" OR 
b.dates LIKE "2017-10-15%" OR 
b.dates LIKE "2017-10-16%" OR 
b.dates LIKE "2017-10-17%" OR'; 

回答

3

$字符指示字符串的結尾。

preg_replace("/OR$/", '', $str); 

rtrim($str, ' OR'); 
+1

你的答案爲c orrect ...你能解釋一下嗎? – GYaN

+0

好的....謝謝 – GYaN

2

這可能會爲你工作:

strrpos - 查找字符串中的子字符串的最後出現的位置

$string = 'b.dates LIKE "2017-10-13%" OR 
b.dates LIKE "2017-10-14%" OR 
b.dates LIKE "2017-10-15%" OR 
b.dates LIKE "2017-10-16%" OR 
b.dates LIKE "2017-10-17%" OR';  


echo substr_replace($string,'', strrpos($string, 'OR'), 2); 

OUTPUT:

b.dates LIKE「2017-10-13%」或b.dates LIKE「2017-10-14%」或b.dates LIKE「2017-10-15%」或b.dates LIKE「2 017-10-16%」或類似 b.dates 「2017年10月17日%」

工作演示:http://codepad.org/rw3q40mk

2

究竟刪除最後3個字符

substr($string, 0, -3); 
-1

你可以使用內置在PHP str_replace()函數功能從如下字符串除去所有的次數。

$res = str_replace("OR", "",$string); 
echo ($res); 

結果:

b.dates LIKE 「2017年10月13日%」

b.dates LIKE 「2017年10月14日%」

b.dates LIKE 「2017年10月15日%」

b.dates LIKE 「2017年10月16日%」

b.dates LIKE 「2017年10月17日%」

要移除的最後一次出現,或使用下面的代碼

echo substr($string, 0,strrpos($string,"OR")); 

結果 b.dates LIKE「二零一七年十月十三日% 「OR b.dates LIKE」2017-10-14%「OR b.dates LIKE」2017-10-15%「OR b.dates LIKE」2017-10-16%「OR b.dates LIKE」2017-10- 17%」

更多信息請訪問http://php.net/manual/en/function.str-replace.php

+0

它會刪除字符串中的所有OR ... – GYaN

+0

仔細閱讀問題...我想刪除最後一次或不是全部。 – GYaN

+0

是這是我的錯,但我已經更新了最後一次出現 –

相關問題