All TalkersCode Topics

Follow TalkersCode On Social Media

PHP Remove Special Characters From String Except Space

Last Updated : Jul 1, 2023

PHP Remove Special Characters From String Except Space

In this tutorial we will show you the solution of PHP remove special characters from string except space, as we know manually we can remove special character but when we go with manually means it’s really has long program and it has too complexity when handling, needs to spend more time on that.

When we use inbuilt function in php it saves time and has no complexity for handling. Here we used preg_replace() inbuilt function for removing special characters except space on string.

Step By Step Guide On PHP Remove Special Characters From String Except Space :-

For removing special characters except whitespace we used preg_replace() function in php.

The preg_replace() function returns a string or array of strings where all matches of a pattern or list of patterns found in the input are replaced with substrings.

We can use this function either defined pattern or replacement string. The preg_replace() will work on regular expression matching so we can easily define with pattern to cover all special characters except whitespace removed from string.

$str = "!@#hello world*^*$";
$res = preg_replace('/[^A-Za-z0-9 ]/','',$str);
echo "Actual String : ".$str;
echo "<br>Replaced String : ".$res;
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. We defined string and stored to variable ‘$str’ and ‘$’ symbol must add before variable name when we define a variable in php.
  4. Our user defined variable value is '!@#hello world*^*$' those all user defined special characters in ‘$str’ needs to remove except whitespace, string and we can give any string with any special characters it’s our own choice.
  5. Variable ‘$res’ contain removed string by using preg_replace() function with three parameters, needed characters pattern except all replace with ‘ ’ null as second parameter and our input string variable ‘$str’ so it returns removed string to variable ‘$res’.
  6. Here we used echo for printing result on webpage browser first echo prints input string then another echo prints removed string as a result on webpage.
  7. In php we have more inbuilt functions for display value or string on webpage.
  8. Using preg_replace() function our input string ‘!@#hello world*^*$’ replaced with special character ‘ ’ is null so the result string is ‘hello world’.
  9. For those string or special characters, we can use any other available special characters with any string not mandatory to use same as above.
  10. But needs to replace special character pattern definition is must give without mistake otherwise it fails to remove.

Conclusion :-

In conclusion we are able to know how to remove space from string before and after in php. When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser it returns removed string without special character as a result.

For display on webpage value or string we can use print, alert box or print_r also available in php.

I hope this tutorial on PHP remove special characters from string except space helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials