JCBiggar
JCBiggar

Reputation: 2507

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!

Upvotes: 3

Views: 1757

Answers (6)

Alex
Alex

Reputation: 1309

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.

Upvotes: 1

bharadwaja Gummadi
bharadwaja Gummadi

Reputation: 305

// 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,

Upvotes: 0

deceze
deceze

Reputation: 522636

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);

Upvotes: 2

patrick
patrick

Reputation: 11731

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

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324840

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));

Upvotes: 8

Krish R
Krish R

Reputation: 22741

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);

Upvotes: 3

Related Questions