Remove special characters regex 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

    • Use the PHP htmlspecialchars() function
  • Method 1 Preg_replace PHP Function
    • Syntax Of Preg_replace PHP Function
    • Remove Special Character From String In PHP
    • How To Call Method
  • Method 2 Str_replace PHP Function
    • Syntax Of Str_replace PHP Function
    • Example To Remove Special Character From String In PHP
    • How To Call Method
  • Clean String for SEO Friendly URL
    • Results
  • Remove ALL non-alphanumeric characters
    • Results
    • Results
  • complete PHP string-cleaning function
    • Results
    • Related posts

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,

  • Remove Special Characters From String Php
  • MySQL Remove Special Characters From Database
  • Remove Null Values From Array In PHP

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.

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");

output:

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.

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");

output:

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.

<?php
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);

Results:

Welcome-to-Pakainfocom-for-free-download-php-projects

Remove ALL non-alphanumeric characters

<?php

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

//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);
var_dump($user_input);

Results:

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.
<?php
$user_input = "mr-changadash()*&>>>>";
$user_input = preg_replace("/[^A-Za-z0-9-]/", '', $user_input);
var_dump($user_input);

Results:

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;
<?php

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'");

Results:

angulark

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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

How remove all special characters from a string in PHP?

This should do what you're looking for: function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars. }

How do I remove special characters from a string?

Example of removing special characters using replaceAll() method.
public class RemoveSpecialCharacterExample1..
public static void main(String args[]).
String str= "This#string%contains^special*characters&.";.
str = str.replaceAll("[^a-zA-Z0-9]", " ");.
System.out.println(str);.

How do you check if a string contains a special character in PHP?

Answer: Use the PHP strpos() Function You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .

How do I allow special characters in PHP?

The htmlspecialchars() function is used to converts special characters ( e.g. & (ampersand), " (double quote), ' (single quote), < (less than), > (greater than)) to HTML entities ( i.e. & (ampersand) becomes &amp, ' (single quote) becomes &#039, < (less than) becomes &lt; (greater than) becomes &gt; ).