Jump to content

trim() and rtrim()


bgsomers

Recommended Posts

The code

echo "buffer--|".$buffer."|--end<br/>";
trim($buffer, " ");
echo "trimmed buffer--|".$buffer."|--end<br/>";
rtrim($buffer, " ");
echo "rtrimmed buffer--|".$buffer."|--end<br/>";

leads to the output

buffer--|term1 10 |--end
trimmed buffer--|term1 10 |--end
rtrimmed buffer--|term1 10 |--end

The descriptions of trim() and rtrim() do not suggest that they will leave a trailing space in the string processed.

How should one understand this situation?

Bruce

(Sorry - I can't see what is limiting the length of the code lines above.)
Link to comment
Share on other sites

You are not changing the value of buffer! trim() returns a trimmed value, but you are not assigning it to anything or using it.

You need to do this:
[code]echo "buffer--|".$buffer."|--end";
$buffer = trim($buffer, " ");
echo "trimmed buffer--|".$buffer."|--end";
$buffer = rtrim($buffer, " ");
echo "rtrimmed buffer--|".$buffer."|--end";
[/code]

Or this:
[code]echo "buffer--|".$buffer."|--end";
echo "trimmed buffer--|".trim($buffer, " ")."|--end";
echo "rtrimmed buffer--|".rtrim($buffer, " ")."|--end";
[/code]

Edit: Also, spaces are included in the "trimmed" character list be default. You don't need to include it in the function as the 2nd parameter.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.