PHP rtrim and ltrim are trimming more than supposed to

Im wanting to remove the Search - from a page on my website. Here is an example of my code below:

Input:

$search = "Search - echelon";
$trim = "Search - ";

$result = ltrim($search,$trim);


echo $result;

The Output:

lon

Desire Output:

echelon

How can I do this, and why does ltrim trim off more in my example above? Thanks!

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

RTM. The second argument is treated as a set of characters to trim.

In this case:

S - in the list, trim it
e - in the list, trim it
a - in the list, trim it
r - in the list, trim it
c - in the list, trim it
h - in the list, trim it
_ - (space) in the list, trim it
- - in the list, trim it
_ - (space) in the list, trim it
e - in the list, trim it
c - in the list, trim it
h - in the list, trim it
e - in the list, trim it
l - NOT in the list, stop!

lon is left

Did you mean this?

$result = substr($search,strlen($trim));

Solution 2

ltrim ( string $str [, string $character_mask ] ) – Strip whitespace (or other characters) from the beginning of a string. character_mask – characters you want to strip

How about str_replace,

$result = str_replace($trim,"",$search);

Solution 3

This is a little late. But all excuses aside.
I have found PHP trim to remove too much quite often. That much that I found it unreliable to use. It simply is a PHP bug that has gone by unattended.

Hence therefore I am using :

function phptrim($str, $strip = ' '){
    while($str != '' && substr($str, 0, 1) == $strip)  $str = substr_replace($str, '', 0, 1);
    while($str != '' && substr($str, -1) == $strip)    $str = substr_replace($str, '', -1);

    return $str;
}

Note that this is a little CPU intensive if you are using this in a loop. So try to avoid needing it. But if you need it. Here it is.

Solution 4

from PHP.net: ltrim — Strip whitespace (or other characters) from the beginning of a string

So it doesn’t trim the string, it trims any of the characters you entered…

I would go with @krishR’s answer

Solution 5

trim trims off any of the characters that you give it. It looks at each character individually and trims it off, it doesn’t search for the string as a whole. If you’re looking to remove a string from the start if and only if it exist, do this:

$trimmed = preg_replace('/^Search - /', '', $search);

Solution 6

// trim — Strip whitespace (or other characters) from the beginning and end of a string
// Example 
$ageTypes = ' Adult, Child, Kids, ';
// Output, removes empty string from both ends 
// Adult, Child, Kids,

// Rtrim  - Remove characters from the right side of a string: 
// Example 
$ageTypes = 'Adult, Child, Kids';
echo rtrim($ageTypes, ',');
// Output 

// Ltrim - Remove characters from the left side of a string: 
// Example 
$ageTypes = ',Adult, Child, Kids, ';
echo ltrim($ageTypes, ',');
// Adult, Child, Kids,

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply