MyBB Community Forums

Full Version: Remove all special characters from a string?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This code only strips the characters provided in the code but I would like to remove all of them leave only numbers and letters!

Please help me to combine these codes:

                $stags = strip_tags($thread['subject']);
		$tagsht = htmlspecialchars_uni($stags);
		$codes = "{ } [ ] ( ) \" ' < > : ; . , - _ + * ! ? = $ % & ¡ ¿ /"; //here should remove all special characters
		$searchcodes = explode(" ",$codes);
		$rtags = str_replace($searchcodes, " ", $tagsht);
		$rtagsp_br = preg_replace("/\r\n|\r/", " ", $rtags);
		$tags = trim($rtagsp_br);
		$explode = explode(" ", $tags);
		$tags = array();
		$countags = 0;
		foreach($explode as $strlen)


This is what I think will remove all of them but not sure how to combine it:


$output = preg_replace("/[^A-Za-z0-9]/","",$input);


This is what I have come up with is it correct?


$stags = "This code only strips the characters provided in the code but I would like to remove all of them leave only numbers and letters!
";
		$tagsht = htmlspecialchars_uni($stags);
		$rtags = preg_replace("/[^A-Za-z0-9]/", " ",$tagsht);
		$rtagsp_br = preg_replace("/\r\n|\r/", " ", $rtags);
		$tags = trim($rtagsp_br);
		$explode = explode(" ", $tags);
		$tags = array();
		$countags = 0;
		foreach($explode as $strlen)
It's not correct, you're stripping all spaces before trying to explode the tags by a space character.

Something like this should work though:
$rtags = trim(preg_replace("/[^0-9a-zA-Z ]/", "", $tagsht));
        $explode = explode(" ", $rtags);
        $tags = array();
        $countags = 0;
        foreach($explode as $strlen)
Buddy you are my hero! Is there something you don't know?