Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trim text using PHP

Tags:

php

trim

How to trim some word in php? Example like

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

I want to remove the last OR

like image 341
wow Avatar asked Nov 27 '22 23:11

wow


2 Answers

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.

like image 132
Paige Ruten Avatar answered Dec 05 '22 15:12

Paige Ruten


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'
like image 38
Russell Dias Avatar answered Dec 05 '22 15:12

Russell Dias