I have this simple on how Function trim() work, but it is not working. Can someone tell me why?

If you notice on the code below in first input tag where my name goes I left planty of spaces at the left side of my name (Masood). I assume the trim() should take them out. I have run the code and show the result at the end of this posting.

<html>
<head>
  <title>Bob's Auto Parts - Customer Feedback</title>
</head>
<body>
<h1>Customer Feed Back</h1><br />
Please tell us what you think

<form action="processfeedback2.php" method="post">
  Your name <br />				
  <input type="text" name="name" value = "              Masood" size="40" /><br />
  Your email address<br />
  <input type="text" name="email" value='masood131@yahoo.com' size="40" /><br />
  Your feedback<br />
  <textarea rows="5" cols="30" name="feedback" value="This is   just a   test"></textarea><br />
  <input type="submit" value="Send feedback" size="40" />
</form>
</body>
</html>

Here is the processcode2, which processes feedback.html


<?php

//create short variable
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];

?>

<html>
<head>
  <title>Bob's Auto Parts- Feedback Submitted</title>
</head>
<body>
<h1>Feedback Submitted</h1>
<p>Your feedback has been sent.</p>

<?php
echo "This is name ".'<strong><u>'.before.'</u></strong>'. " running the function trim() <br />";
echo $name.'<br /><br />';
echo "This is name ".'<strong><u>'.after.'</u></strong>'. " running the function trim() <br />";
$name = trim($name);
echo $name;
?>
</body>
</html>

and here is what I get as a result:

Feedback Submitted
Your feedback has been sent.

This is name before running the function trim() 
Masood

This is name after running the function trim() 
Masood 

Ty

masood

    well there are no spaces before the word Masood.... how do you know its not working?

    in fact it is. it just happens to be that spaces in a form element like this;

    <input type="text" name="name" value = " Masood" size="40" /><br />

    dont show up either.

      as far as I can see, it actually does strip the spaces from the beginning, otherwise you should see this;

      Before : Masood
      After : <space><space><space>Masood

      or something to that effect

      because the results are both equal it's stripped the spaces.

        I guess I don't understand why is the name before running the trim() function does not have any spacing before it when I print it out. It looks exactely the same before and after.

        masood

          The reason you don't see the spaces preceeding the name before trimming is because your web browser doesn't display whitespace. It is just part of the standard html viewing spec. If you look at the page source you will see the spaces. Here is a cut and paste to illustrate:

          <html>
          <head>
            <title>Bob's Auto Parts- Feedback Submitted</title>
          </head>
          <body>
          <h1>Feedback Submitted</h1>
          <p>Your feedback has been sent.</p>
          
          This is name <strong><u>before</u></strong> running the function trim() <br />              Masood<br /><br />This is name <strong><u>after</u></strong> running the function trim() <br />Masood</body>
          
          </html>
          

          Hope that clears it up.

          _kai

            Write a Reply...