我只想問,我想從後續字符串中刪除最後的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