PHP Replace Special Characters
Last Updated : Jan 1, 2023
IN - PHP
In this tutorial we will show you the solution of PHP replace special characters, as we know manually we can replace 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 str_replace() inbuilt function for replacing special characters on string.
Step By Step Guide On PHP Replace Special Characters :-
For replacing special characters we used str_replace() function in php. The str_replace() function replaces some characters with some other characters in a string.
If the string to be searched is an array, find and replace is performed with every array element.
In our user defined string we defined with one special character that’s replaced with another special character by str_replace() function.
This function contains three parameters those are ‘which character needs to replace, to be replace character, user defined string’.
<?php $str = "**hello world**"; $res = str_replace('*','@',$str); echo "Actual String : ".$str; echo "<br>Replaced String : ".$res; ?>
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
- The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
- We defined string and stored to variable ‘$str’ and ‘$’ symbol must add before variable name when we define a variable in php.
- Our user defined variable value is '**hello world**' that ‘*’ is we defined special character and we can give any string with any special character it’s our own choice.
- Variable ‘$res’ contain replaced string by using str_replace() function with three parameters, *-needs to replace with ‘@’ second parameter and our input string variable ‘$str’ so it returns replaced string to variable ‘$res’.
- Here we used echo for printing result on webpage browser first echo prints input string then another echo prints replaced string as a result on webpage. In php we have more inbuilt functions for display value or string on webpage.
- Using str_replace() function our input string ‘**hello world**’ replaced with special character ‘@’ so the result string is ‘@@hello world@@’.
- For ‘*’ and ‘@’ we can use any other available special characters not mandatory to use same as above. But needs to replace special character must presence in our input otherwise it fails to replace.
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 replaced string with ‘@’ special character as a result.
For display on webpage value or string we can use print, alertbox or print_r also available in php.
I hope this tutorial on PHP replace special characters helps you and the steps and method mentioned above are easy to follow and implement.