YogeshChauhan.com
How to remove special characters (dash, asterisk etc) from any string in PHP?
January 5, 2021

There are many ways to use str_replace function.

Let’s take a look at the function first.

preg_replace

The preg_replace() function performs a regular expression searches a string for matches to a pattern and replaces them with replacement.

Syntax:


preg_replace(mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) : mixed

Simple Syntax:


preg_replace(the value to find, the value to replace, string to be searched, the maximum possible replacements for each pattern, the number of replacements)

Where first 3 parameters are required.

str_replace

The str_replace() function replaces some characters with some other characters in a string.

Syntax:


str_replace(mixed $search , mixed $replace , mixed $subject [, int &$count ] ) : mixed

Simple Syntax:


str_replace(the value to find,the value to replace,string to be searched,the number of replacements)

Where first 3 parameters are required.

strtolower

It makes the string lowercase.

Remove all special characters from a string


$cleanString = strtolower($string); 
$cleanString = preg_replace('/[^a-z0-9 -]+/', '', $cleanString);
$cleanString = str_replace(' ', '-', $cleanString);

OR make a function so that you can use it number of times


function cleanInputs($string) {
   $string = str_replace(' ', '-', $string); 
   return preg_replace('/[^A-Za-z0-9-]/', '', $string); 
}

It’s up to you if you want to remove the hyphens first or after removing the special characters.

NOTE: You can replace it with lowercase or any other letter you like. I’ve used hyphens just for example.

There is a problem though. The solution above leaves multiple hyphens in the solution.

Replace multiple hyphens with one 


$finalOutput = preg_replace('/-+/', '-', $string); 

How to remove dash form any string?


$stringWithoutDash = str_replace("-", " ", $string);

You can remove any special characters you like. One at a time if your code requires that.


$string = str_replace("-", " ", $string);
$string = str_replace("?", " ", $string);
$string = str_replace("!", " ", $string);

Just like above example, it won’t replace all the special characters but only 3.

Sources

dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 2 the box-sizing property in CSS Mar 2 WordPress: How to access first and random row values from a repeater field in ACF? Mar 1 The basics of CSS Box model Feb 27 WordPress: How to display slider repeater fields in ACF? Feb 26 Simple Page Hit Counter in PHP Feb 26 Here’s what we can do with PHP date() function

You might also like these

Clearwater Seafoods – B2C in ChinaMiscHow to create a pricing table using flex in CSS?CSSAggregate Functions Examples in Postgres for BeginnersPostgresHow to Hide HTML elements automatically after few seconds using JavaScript?HTMLHow to create a simple tab interaction using CSS?CSS3 Common Usability Mistakes In Web DesignUI/UX