4

How to trim some word in php? Example like

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

I want to remove the last OR

wow
  • 7,989
  • 17
  • 53
  • 63

5 Answers5

12

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.

Paige Ruten
  • 172,675
  • 36
  • 177
  • 197
7

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
  • 70,980
  • 5
  • 54
  • 71
  • The author does not state his intentions... and it answers the question clearly. – Russell Dias Jun 19 '10 at 07:22
  • 1
    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? – Your Common Sense Jun 19 '10 at 07:23
  • 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 Jun 19 '10 at 07:25
  • author's intentions is **clear:** to get rid of **word** "OR", not characters O and R. Your solution is terrible for the programmer. – Your Common Sense Jun 19 '10 at 07:26
  • 2
    Another SO-style answering robot. – Your Common Sense Jun 19 '10 at 07:27
  • 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 Jun 19 '10 at 07:28
  • yeah, there is nothing to argue – Your Common Sense Jun 19 '10 at 07:31
  • 5
    @Col. Shrapnel: Having a bad day? – soulmerge Jun 19 '10 at 07:49
  • 4
    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 trimming the word "OR" off of the right end of the $string. 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. – Paige Ruten Jun 19 '10 at 07:51
  • 2
    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 Jun 19 '10 at 08:03
  • 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 Jun 19 '10 at 08:08
  • 2
    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 Jun 19 '10 at 08:09
4

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
  • 90,431
  • 16
  • 141
  • 175
2

A regular expression would also work:

$str = 'pid="5" OR pid="3" OR';
print preg_replace('/\sOR$/', '', $str);
Mike
  • 21,301
  • 2
  • 42
  • 65
  • While it would work, it's more complicated and computationally expensive than necessary – Brad Mace Sep 07 '11 at 14:22
  • @bemance: I can't argue with that. A quick test of 1 million repetitions on my PC shows: `preg_replace` 2428ms, `rtrim` 624ms, `substr` 604ms, `implode` 660ms - `preg_replace` is approximately 4 times slower than the other solutions. – Mike Sep 07 '11 at 19:03
1

What do you think about this?

$str='pid="5" OR pid="3" OR';    
$new_str=substr($str,0, strlen($str)-3);
Bakhtiyor
  • 7,198
  • 15
  • 55
  • 77