John Mwaniki /   19 May 2023

How to remove a certain character from a string in PHP

When working with PHP, you are likely to encounter a scenario where there is a need to remove all the occurrences of a specific character from a string. This could be in eliminating unwanted characters from user input or manipulating data among other scenarios.

In this article, we will explore various ways in which you can remove a certain character from a string in PHP with a working example of each.

Method 1: Using str_replace() function

The str_replace() function replaces all the occurrences of specific character(s) within a string with a replacement string. It returns the resulting string with the replaced values.

Syntax

str_replace(find,replace,string,count)

Parameters Description

Parameter Requirement Description
find Required Specifies the value we want to find and replace.
replace Required Specifies the value to replace the value in find with.
string Required Specifies the string whose characters we want to replace.
count Optional If passed, it specifies the number of replacements to be performed.

To remove a certain character, you simply specify it in the find parameter and replace it with an empty string.

Example 1

Removing all the occurrences of hyphens (-) from a phone number string.

<?php
$phone = "123-456-7890";
$phone = str_replace('-', '', $phone);
echo $phone;
?>

Output:

1234567890

Example 2

To remove the occurrences of multiple characters or phrases in a string, we specify them as an array in the find parameter.

<?php
$string = "Merlin, (Frodo), 'Hercules'";
$newstring = str_replace(array("(", ")", "'"), "", $string);
echo $newstring;
?>

Output:

Merlin, Frodo, Hercules

In this example, we wanted to clean the string by removing the occurrences of (, ), and ' all at once. So we have passed an array of the three characters array("(", ")", "'") in the find parameter and replaced it with an empty string "".

Method 2: Using preg_replace() function

The preg_replace() function performs a regular expression search and replace, and returns a resulting string.

Syntax

preg_replace(pattern, replacement, string, limit, count)

Parameters Description

Parameter Requirement Description
pattern Required Specifies a regular expression to search and find occurrences in the string that we want to replace.
replacement Required Specifies a replacement string to replace the value matched by the pattern.
string Required Specifies the string whose characters we want to replace.
limit Optional Specifies the number of replacements to be performed. If not passed, it defaults to -1, meaning unlimited replacements.
count Optional If specified, this variable will contain the number of replacements done.

To remove a specific character using preg_replace() function, we pass a regular expression pattern that matches the character and replace it with an empty string "".

Example 1

<?php
$phone = '123-456-7890';
$phone = preg_replace('/-/', '', $phone);
echo $phone;
?>

Output:

1234567890

In this example, we have defined our regular expression to match the occurrences of a hyphen (-) by placing it in between two forward slashes /-/.

Example 2

To remove the occurrences of multiple characters in a string using this method, we include them in our regular expression.

<?php
$string = "Merlin, (Frodo), 'Hercules]'";
$newstring = preg_replace("/[()']/", "", $string);
echo $newstring;
?>

Output:

Merlin, Frodo, Hercules

In this example, we have defined our regular expression to match the occurrences of (, ), and ' by placing them in square brackets in between two forward slashes /[()']/.

Method 3: Using str_split() and implode()

The str_split() function splits a string into an array.

Syntax

str_split(string,length)

The string parameter specifies the string to split while length is an optional parameter that specifies the length of each array element in the resulting array. If the length parameter is not passed, its value defaults to 1, meaning each array element will have one character.

The implode() function joins array elements into a string and returns the resulting string.

Syntax

implode(separator,array)

The separator is an optional parameter that specifies what to put between the array elements in the resulting string. When not passed, it defaults to an empty string "". The array parameter specifies the array to join to a string.

Removing a character from a string using this method involves splitting the string into an array of characters using the str_split() function, filtering out the unwanted character, and then joining the remaining characters back into a string using implode() function.

Example

<?php
$phone = "123-456-7890";
$phoneArray = str_split($phone);
$phoneArray = array_filter($phoneArray, function ($char) {
    return $char !== '-';
});
$phone = implode('', $phoneArray);
echo $phone;
?>

Output:

1234567890

Conclusion

Removing a certain character from a string in PHP is a common task in various projects. In this article, we have explored three different methods to accomplish the desired outcome.

Choose the one that best suits your specific requirements and coding style. Happy coding.