Yogesh Chauhan's Blog

How to remove special characters (dash, asterisk etc) from any string in PHP?

in PHP on January 5, 2021

There are many ways to use str_replace function.

Let’s take a look at the function first.


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


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.


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


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.


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.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to catch .keypress() on body using jQuery?jQueryJavaScript Data Types and Data Structures Things to RememberJavaScriptJavaScript: Methods for HTML DOM classList PropertyJavaScriptHow to enable and disable button using JavaScript?JavaScriptWordPress: How to get ACF field values from another post?WordPressHow to draw an SVG path on scroll using JavaScript?JavaScript