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
https://forums.phpfreaks.com/topic/34272-trim-and-rtrim/
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
https://forums.phpfreaks.com/topic/34272-trim-and-rtrim/#findComment-161186
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.