Php

Remove Special Character in PHP Using str_replace()

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.

Simple Example to Escape special characters in PHP

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.

Using str_replace PHP Function

The 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

Related Post
str_replace ( $searchVal, $replaceVal, $subjectVal, $count )

The Parameters are:

  • searchVal(Required) : This parameter specifies the string to be searched and replaced. It can be either a string or an array with strings.
  • replaceVal(Required) : This parameter specifies the string with which we want to replace the searchVal string. It can be either a string or an array with strings.
  • subjectVal(Required) : This parameter specifies the string or array of strings which we want to search for searchVal and replace with replaceVal. It can be either a string or an array with strings.
  • $count(Optional) : This parameter is optional and if passed, its value will be set to the total number of replacement operations performed on the string subjectVal.
//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

Remove Special Characters Using Regular Expression

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:

  • patterns(Required) : Contains a regular expression or array of regular expressions
  • replacements(Required) : A replacement string or an array of replacement strings
  • input(Required) : The string or array of strings in which replacements are being performed
  • limit(Optional) : The defaults is -1, means unlimited. Sets a limit to how many replacements can be done in each string.
  • count(Optional) : After the function has executed, this variable will contain a number indicating how many replacements were performed.
$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

References:

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

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

4 months ago

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

10 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

10 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

10 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

11 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

11 months ago

Categories