-2

I have the next situation:

$a = '   0226  ';

I'm trying to remove whitespaces from the beginning and end of the string:

print_r(trim($a));

and expected output is:

'0226'

Here are results of var_dump and urlencode of above string:

print_r(urlencode($a)); // %C2%A0+0226+%C2%A0
var_dump($a) // <pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'  0226  '</font> <i>(length=10)</i>
</pre>
Haris Hajdarevic
  • 1,535
  • 2
  • 25
  • 39

2 Answers2

0

Your code does not alter $a in any way.

The syntax for a trim() is

$newvar = trim($originalvar);

Contatenating functions like you have means you never actually store the trimmed value anywhere that you can later dump it to check its value.

Try var_dumping the value you actually trimmed

$a = '   0226  ';
$b = trim($a);
echo 'Trimmed value = >' . $b . '<';

$c = urlencode($b);
echo 'urlencoded value = >' . $c . '<';

Also print_r() is for dumping arrays and I dont think any of your code creates or uses an array.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

The space preceding the text and after the text isnt a kind of space trim can work on. I think there is more to the space than just what you see.

You could use htmlspecialchars() to detect if there are special characters other than spaces before and after the text...