Php

Remove Special Character from String in PHP

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:

Using preg_replace PHP Function

We 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.

Syntax of preg_replace PHP Function:

$result = preg_replace($pattern, $replaceWith , $string);

The Parameters are:

Related Post
  • $pattern: The pattern to search for. It can be either a string or an array with strings.
  • $replaceWith: The string or an array with strings to replace. If this parameter is a string and the pattern parameter is an array, all patterns will be replaced by that string. If both pattern and replacement parameters are arrays, Each pattern will be replaced by the replacement counterpart.
  • $string: This is the string for which you want to filter special characters.
  • $result: This is the output string without special characters.

Remove Special Character from String in PHP Using preg_replace()

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.

How to Call Method

echo RemoveSpecialChar("does't happened ' ' test");

Output:

does t happened test

Using str_replace PHP Function

The 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.

Syntax of str_replace PHP Function:

$title = str_replace( array( '\'', '"', ',' , ';', '<', '>' ), ' ', $value);

Remove Special Character from String in PHP Using str_replace()

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.

How to call Method

echo RemoveSpecialChar();

Using filter_var

PHP 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.

Conclusion

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.

Recent Posts

What is the Purpose of php_eol in PHP?

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

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

3 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories