tags:

views:

187

answers:

5

How to trim some word in php? Example like

pid="5" OR pid="3" OR

I want to remove the last OR

+8  A: 

You can try rtrim:

rtrim — Strip whitespace (or other characters) from the end of a string

$string = "pid='5' OR pid='3' OR";
echo rtrim($string, "OR"); //outputs pid='5' OR pid='3'
Russell Dias
The author does not state his intentions... and it answers the question clearly.
Russell Dias
at least substr() would be more adequate to the *goal*, not a literal question. Don't blame author. Don't you have your own brains to think with?
Col. Shrapnel
His *goal* is to simply get rid of the last OR. Which this does. If you have a solution then please feel free to post it...
Russell Dias
author's intentions is **clear:** to get rid of **word** "OR", not characters O and R. Your solution is terrible for the programmer.
Col. Shrapnel
Another SO-style answering robot.
Col. Shrapnel
As stated earlier, feel free to post your solution. I do not wish to waste my time with petty arguments about 'my brains'.
Russell Dias
yeah, there is nothing to argue
Col. Shrapnel
@Col. Shrapnel: Having a bad day?
soulmerge
using rtrim() is fine here, a bit clever (and probably influenced by the wording of the question), and shows the statement's purpose a lot more clearly than substr() would. We're <b>trim</b>ming the word <b>"OR"</b> off of the <b>r</b>ight end of the <b>$string</b>. It does look like rtrim() is working at the word level rather than the char level (I didn't even realize this until Col Shrapnel finally spelled it out), but I think in this case, where you know your string will always end in " OR", it's a great way to trim it off.
yjerem
jeremy, you only know that now. There's no guarantee that a maintainer a few years down the road notice this hack. A clear, self-contained solution without quirks or side-issues is often best for maintainability. Your own solution is an example of an excellent solution, where it's easy to add more arguments to the query without breaking anything. rtrim() is not that excellent, because it does have quirks and non-obvious side effects.
Emil Vikström
I agree with Jeremy's solution as it provides the maintainability element, which mine lacks. I would rather get an explanation as to why mine is bad (thanks for that Emil) rather than just negative criticism.
Russell Dias
rtrim() will remove 'OR' from the end, but it will also remove 'RO', or just 'R' or just 'O', if that is what the string happens to end with. It might not be the most robust solution, but it works, and I don't think it is quite as evil as it has been made out to be.
Mike
+11  A: 

I suggest using implode() to stick together SQL expressions like that. First build an array of simple expressions, then implode them with OR:

$pids = array('pid = "5"', 'pid = "3"');

$sql_where = implode(' OR ', $pids);

Now $sql_where is the string 'pid = "5" OR pid = "3"'. You don't have to worry about leftover ORs, even when there is only one element in $pids.

Also, an entirely different solution is to append " false" to your SQL string so it will end in "... OR false" which will make it a valid expression.

@RussellDias' answer is the real answer to your question, these are just some alternatives to think about.

yjerem
+1 for building the argument list as an array.
Emil Vikström
+3  A: 

A regular expression would also work:

$str = 'pid="5" OR pid="3" OR';
print preg_replace('/\sOR$/', '', $str);
Mike
+4  A: 

Using substr to find and remove the ending OR:

$string = "pid='5' OR pid='3' OR";
if(substr($string, -3) == ' OR') {
  $string = substr($string, 0, -3);
}
echo $string;
Emil Vikström
A: 

What do you think about this?

$str='pid="5" OR pid="3" OR';    
$new_str=substr($str,0, strlen($str)-3);
Bakhtiyor