Jump to content


Photo

Removing unsafe characters from a string?


  • Please log in to reply
2 replies to this topic

#1 Switch0r

Switch0r
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts
  • LocationManchester, UK

Posted 24 April 2006 - 08:09 AM

does anyone know of a function (inbuilt or otherwise) that can strip all of the unsafe chars from a string? ie. ; : " ' ...etc in one go? or will i have to just keep using the str_replace function over and over?
The Human Torch was denied a bank loan...

#2 slashemail

slashemail
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 24 April 2006 - 09:00 AM

I would suggest you a combination of functions like

htmlspecialchars
strip_tags
stripslashes
mysql_escape_string        #For inserting into the database(Mysql)

Hope this helps

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 April 2006 - 10:21 AM

You can use preg_raplace like so:
<?php

$string = "This \"string\" has; : got some 'unsafe' chars!";
$string2 = preg_replace("([;:\"'])", "", $string);

echo $string2;

?>
Or you can use str_replace like so:
<?php

$string = "This \"string\" has; : got some 'unsafe' chars!";

$string2 = str_replace(array(";", ":", "\"", "'"), "", $string);

echo $string2;

?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users