Today, I will discuss how to remove special characters from string in php using str_replace()
, In order to do this task, PHP have str_replace()
function to remove special characters from string str in PHP.
Normally programmers are using regular expressions to remove special characters from string with the help of preg_replace()
method.
You can also check other tutorial of PHP string:
Let’s create PHP method to remove special characters from strings.
We’ll use str_replace method to remove special characters from a string, I have created a config.php
file that will have escapeSequenceValidation
variable to store all special characters, that need to remove from the source string.
str_replace
PHP FunctionThe str_replace() is a built-in function in PHP and is used to replace all the occurrences of the search string. This function returns a string or an array.
Syntax:
str_replace ( $searchVal, $replaceVal, $subjectVal, $count )
The Parameters are:
//target string $value ="Hi, I am ~ parvez $ alam"; echo 'before = ' .$value; //get escape string from config file separated with ':' //ex - ~:$ echo $escapeChars = $config['escapeSequenceValidation']; //if exist if($escapeChars) { //explode string with delimiter $arrEscapeChars = explode(':', $escapeChars); print_r($arrEscapeChars); //replace escape characters array values with null values $value = str_replace($arrEscapeChars, '', $value); } echo 'after = '.$value;
Output:
before = Hi, I am ~ parvez $ alam ~:$ Array ( [0] => ~ [1] => $ ) after = Hi, I am parvez $ alam
We can also use preg_replace()
method remove special characters from string in php. It’s used to perform a regular expression search and replace. This function searches for pattern in subject parameter and replaces them with the replacement.
The preg_replace()
method is a built-in function of PHP that also remove special character from string. You can use this method with one array or array of patterns with a replacement string.
preg_replace(patterns, replacements, input, limit, count)
The Parameters are:
-1
, means unlimited. Sets a limit to how many replacements can be done in each string.$str = "Hi, I am ~ parvez $ alam"; function clean($label) { return preg_replace('/[^A-Za-z0-9-]/', ' ', $label); // Removes special chars. } echo clean($str);
Output is:
Hi I am parvez alam
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More