我只想问,我想从后续字符串中删除最后的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';
我只想问,我想从后续字符串中删除最后的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';
$字符指示字符串的结尾。
preg_replace("/OR$/", '', $str);
或
rtrim($str, ' OR');
这可能会为你工作:
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日%”
究竟删除最后3个字符
substr($string, 0, -3);
<?php
$hello ="dddOR";
echo rtrim($hello,"OR");
?>
你可以使用内置在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%”
你的答案为c orrect ...你能解释一下吗? – GYaN
好的....谢谢 – GYaN