Phear46 Posted May 12, 2013 Share Posted May 12, 2013 I want to remove the last ' AND' from my string im using to build a mySQL query. I thought using rtrim was the way to go but either im wrong or im not using it correctly: foreach ($dupArray as $k => $v) { $whereStr = $whereStr . "$k='$v' AND "; } rtrim($whereStr, "AND"); echo $whereStr; Anyone got any ideas? I swear ive seen this done else where but i cant find any examples Quote Link to comment https://forums.phpfreaks.com/topic/277930-remove-substring-from-end-of-string/ Share on other sites More sharing options...
Solution Barand Posted May 12, 2013 Solution Share Posted May 12, 2013 (edited) try $str = "abcde AND "; echo substr($str, 0, -4); // --> 'abcde ' But if you store the WHERE conditions in an array then JOIN with " AND " you can avoid the problem Edited May 12, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/277930-remove-substring-from-end-of-string/#findComment-1429725 Share on other sites More sharing options...
Jessica Posted May 12, 2013 Share Posted May 12, 2013 Your string ends with " AND ". Not "AND". Quote Link to comment https://forums.phpfreaks.com/topic/277930-remove-substring-from-end-of-string/#findComment-1429731 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.