2017-10-11 72 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

是这是我的错,但我已经更新了最后一次出现 –

相关问题