Home » How to Remove all special characters from a string in php?

How to Remove all special characters from a string in php?

In this remove special characters from string php tutorial, We will step by step learn how to remove special character from string in PHP source code. many times – I required to fetch output of an input data string as a easy to use php string replace composition of all the available alphabets as well as numbers with, want to delete all not supported special characters from data string by using php preg_replace.

Use the PHP htmlspecialchars() function

all the available characters like as (&, ", ', <, >) have special significance in HTML, as well as should be converted to HTML data entities. also You can use the PHP main inbuilt htmlspecialchars() function to convert data special characters in a string to HTML contententities. so here setp by step Let’s check out php trim an example:

You can also check other tutorial of string,

There are Best two Method support to remove/delete special character from Data string using php full source code with php remove character from string examples.

Read Also:  How to Check If String Contains a Substring in PHP

Method 1: Preg_replace PHP Function

I can remove or delete special character from sting using preg_replace, As like name its using regular expression to remove or delete special character from string.

Remove Special characters from String php

You can get more php remove character from string details of regular expression from PHP Regular Expression Usage & Example tutorials.

Syntax Of Preg_replace PHP Function:

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

  • $pattern : The change or update pattern to find for. It can be either a data string or an array with strings.
  • $replaceWith= The data string or an array with data strings to replace. If this parameter is a data string as well as the pattern parameter is an array, all patterns will be replaced by that data string. If both pattern and replacement arguments are arrays, each pattern will be replaced by the replacement counterpart.
  • $string = This is the data string which you want to filter special characters.
  • $result = This is the output data string without special characters.

Remove Special Character From String In PHP:

function RemoveSpecialChar($value){
$result = preg_replace('/[^a-zA-Z0-9_ -]/s','',$value);

return $result;

How To Call Method:

echo RemoveSpecialChar("loves't jiorockers ' ' tamil");


loves t jiorockers tamil

Method 2: Str_replace PHP Function

You can also use Extra available new PHP function str_replace() to get the same type of the data result as above source code, if I know what all I have to remove or delete special characters using php str_replace.

Read Also:  AngularJS populate Dynamic Dependent dropdown in PHP

Syntax Of Str_replace PHP Function:

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

Example To Remove Special Character From String In PHP:

function RemoveSpecialChapr($value){
$slogan = str_replace( array( '\'', '"', ',' , ';', '<', '>' ), ' ', $value);

return $slogan;

How To Call Method

echo RemoveSpecialChar("loves't jiorockers ' ' tamil");

loves t jiorockers tamil

Clean String for SEO Friendly URL

The bellow full source code cleans a data string that can be used in the URI some mainsegment in PHP scripts for crating anew SEO friendly URL.

function RemoveSpecialChar($user_input){
// Replaces or deletes all spaces with hyphens.
$user_input = str_replace(' ', '-', $user_input);

// Removes/deletes special chars.
$user_input = preg_replace('/[^A-Za-z0-9\-]/', '', $user_input);
// Replaces or deletes multiple hyphens with single one.
$user_input = preg_replace('/-+/', '-', $user_input);

return $user_input;

$user_input = "Welcome to Pakainfo.com for free download php projects.";
echo $removeSpecialChar = RemoveSpecialChar($user_input);



Remove ALL non-alphanumeric characters


//String containing non-alphanumeric characters
$user_input = "pakainfo*is-a'welcome ()";

//Remove or delete any character that isn't A-Z, a-z or 0-9.
$user_input = preg_replace("/[^A-Za-z0-9]/", '', $user_input);


string(24) "pakainfo*is-a'welcome ()" string(18) "pakainfoisawelcome"

Regex to remove non-alphanumeric characters – except hyphens.

  • String containing special characters.
  • Remove any character that isn’t A-Z, a-z, 0-9 or a hyphen.
Read Also:  How to String Replace in PHP Functions?

$user_input = "mr-changadash()*&>>>>";
$user_input = preg_replace("/[^A-Za-z0-9-]/", '', $user_input);


string(13) "mr-changadash"

complete PHP string-cleaning function

  • Strip all the characters from a string except the alphabet characters [a-zA-Z];.
  • Make the remaining characters lowercase.
  • Limit the resulting string to eight characters;


function cleanString($string)
// allow only letters
$res = preg_replace("/[^a-zA-Z]/", "", $string);

// trim what's left to 8 chars
$res = substr($res, 0, 8);

// create a lowercase
$res = strtolower($res);

// return
return $res;

// demo the function
echo cleanString("[email protected]#$%^&*()-=+_][{};:/.,<>?AAMNBVCXZLKJHG'");



I hope you get an idea about remove special characters from string php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  convert date format in php
  2. Read Also:  Ajax Login System with jQuery PHP and MySQLi
  3. Read Also:  How To Convert String Replace In Php Examples
  4. Read Also:  echo and print difference in php
  5. Read Also:  Export Json Array to CSV using php Example
  6. Read Also:  Simple DateTime range with two inputs Example
  7. Read Also:  How to check if string contains substring in PHP?
  8. Read Also:  How to use multiple databases in Laravel
  9. Read Also:  how to run codeigniter project in xampp
  10. Read Also:  codeigniter count rows in view & controller

Leave a Comment

Your email address will not be published. Required fields are marked *