Skip to main content
June 10, 2009
Answered

Stripping Special Characters out of a User Submitted Variable

  • June 10, 2009
  • 1 reply
  • 554 views

Hello,

For the code below, I would like to strip apostrophes, periods, and slashes from the variable $find if the user enters them in.  How can I do that?  I would like to simply delete these characters, and not replace them with an underscore or anything like that.

Thanks in advance,

John

<div class="searchbox">
  <form action="search.php" method="post">
  <label>Enter Topic:
  <input type="text" name="find" size="55"/>
  <input type="hidden" name="searching" value="yes" />
  <input type="submit" name="search" value="Search" />
  </label>
  </form>
  </div>

This topic has been closed for replies.
Correct answer David_Powers

Use str_replace().

$illegal = array("'", ".", "/");

$find = str_replace($illegal, '', $_POST['find']);

1 reply

David_Powers
David_PowersCorrect answer
Inspiring
June 10, 2009

Use str_replace().

$illegal = array("'", ".", "/");

$find = str_replace($illegal, '', $_POST['find']);

June 11, 2009

Hi David,

Thanks, it works great.  However, I would like to also strip out double quotes, and I am having a hard time getting the code to do that.  How can I?

Thanks,

John

David_Powers
Inspiring
June 11, 2009

Just surround the double quote in a pair of single quotes:

$illegal = array("'", ".", "/", '"');