Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Stripping Special Characters out of a User Submitted Variable

Guest
Jun 10, 2009 Jun 10, 2009

Copy link to clipboard

Copied

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>

TOPICS
Server side applications

Views

500
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jun 10, 2009 Jun 10, 2009

Use str_replace().

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

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

Votes

Translate
LEGEND ,
Jun 10, 2009 Jun 10, 2009

Copy link to clipboard

Copied

Use str_replace().

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

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 10, 2009 Jun 10, 2009

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 11, 2009 Jun 11, 2009

Copy link to clipboard

Copied

LATEST

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

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines