Jump to content

Archived

This topic is now archived and is closed to further replies.

Switch0r

Removing unsafe characters from a string?

Recommended Posts

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?

Share this post


Link to post
Share on other sites
I would suggest you a combination of functions like

[code]
htmlspecialchars
strip_tags
stripslashes
mysql_escape_string        #For inserting into the database(Mysql)
[/code]

Hope this helps

Share this post


Link to post
Share on other sites
You can use preg_raplace like so:
[code]<?php

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

echo $string2;

?>[/code]
Or you can use str_replace like so:
[code]<?php

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

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

echo $string2;

?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.