0

I am attempting to use preg_replace to purge everything except letters and numbers in the $_POST, however is seems to be failing to work.
Sample code below demonstrating the problem.

<html>
<?php
 if( isset($_POST["name"]) || isset($_POST["age"]) )
  {
     $x1 = preg_replace('[^A-Za-z0-9]', "", $_POST['name'] );
     $x2 = preg_replace('[^A-Za-z0-9]', "", $_POST['age'] );
     $_POST['name'] = $x1;
     $_POST['age'] = $x2;
     echo "Welcome ". $_POST['name']. "<br />";
     echo "You are ". $_POST['age']. " years old.";

  }
  else{
  $_POST['name'] = 'null';
  $_POST['age'] = 'null';
  }
?>
<body>
  <form action="<?php $_PHP_SELF ?>" method="POST" onsubmit=" ">
<br>
  Name: <input type="text" name="name" />
  Age: <input type="text" name="age" />

  <input type="submit" />
  </form>
</body>
</html>
2
Contributors
2
Replies
9
Views
3 Years
Discussion Span
Last Post by RLS0812
0

What does the following do?

$x1 = preg_replace('/[^A-Za-z0-9]/', "", $_POST['name'] );
$x2 = preg_replace('/[^A-Za-z0-9]/', "", $_POST['age'] );
echo "Welcome " . $x1 . "<br />";
echo "You are " . $x2 . " years old.";

Looks like you missed the enclosing characters, slashes in this case.

0

That strips everything exept letters and numbers from 'name' and 'age'

Thanks for the info ... didn't notice the missing / /

Edited by RLS0812

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.