In this PHP tutorial, I will discuss how to remove special character from strings in PHP using preg_replace. Strings containing special characters may be vulnerable to security flaws, inconsistent data, or unfavorable results.
Sometimes, We need to get the result of an input string as a simple composition of alphabets and numbers and, want to remove all special characters from the string by using PHP preg_replace method.
There are two options available to remove special characters from a string using PHP.
You can also check another tutorial of PHP strings:
preg_replace PHP FunctionWe can remove special characters from a string using preg_replace, As like naming it using a regular expression to remove special characters from a string.
preg_replace PHP Function:$result = preg_replace($pattern, $replaceWith , $string);
The Parameters are:
function RemoveSpecialChar($value){
$result = preg_replace('/[^a-zA-Z0-9]/s','',$value);
return $result;
} In this example, the regular expression /[^a-zA-Z0-9 ]/ matches any character that is not an uppercase letter, lowercase letter, digit, or space. Those characters will be removed from the string.
echo RemoveSpecialChar("does't happened ' ' test"); does t happened test
str_replace PHP FunctionThe str_replace function in PHP is a simple way to remove specific characters from a string. If we know what special characters we need to remove, we can use str_replace() to get the same effect as the above script.
str_replace PHP Function:$title = str_replace( array( '\'', '"', ',' , ';', '<', '>' ), ' ', $value);
function RemoveSpecialChapr($value){
$string = "Hello, @world!";
// Define an array of special characters to remove
$specialChars = array("@", "!");
// Remove special characters using str_replace
$cleanString = str_replace($specialChars, "", $string);
echo "Source String: $string\n";
echo "Result String: $cleanString\n";
} in the above code, We have created an array $specialChars containing the special characters we want to remove. The str_replace function then replaces these characters with an empty string, resulting in a sanitized version of the original string.
echo RemoveSpecialChar();
filter_varPHP also provides filter_var function, You can combine it with the FILTER_SANITIZE_STRING filter to remove HTML tags and special characters from strings.
<?php $string = "<b>test</b> this & string!"; // Remove HTML tags and special characters using filter_var $cleanString = filter_var($string, FILTER_SANITIZE_STRING); echo "Original String: $string\n"; echo "Cleaned String: $cleanString\n";
FILTER_SANITIZE_STRING removes HTML tags and special characters from the source string.
Removing special characters from strings in PHP is a common operation in web applications. You can use basic method str_replace or preg_replace, or advanced methods involving filter_var and custom regular expressions to remove unwanted characters from strings.
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