remove special characters from string php

Today, We want to share with you remove special characters from string php.In this post we will show you php removes multiple characters from string, hear for removes html special character from string php we will give you demo and example for implement.In this post, we will learn about Use the PHP htmlspecialchars() function with an example.

remove special characters except space from string php

In this tutorial we learn to all about removes special character from string php Examples like as a remove from string php, removes specific character, php remove multiple characters or many more.

Read Also:  MySQL Remove special characters from database

Remove all special characters from a string

function clean($allCleanString) {
   $allCleanString = str_replace(' ', '-', $allCleanString);

   return preg_replace('/[^A-Za-z0-9\-]/', '', $allCleanString);
}

echo clean('a|"[email protected]£de^&$f g');

Using str_replace() Method:

<?php 
function RemoveSpecialChar($allCleanString) { 
	$res = str_replace( array( '\'', '"', 
	',' , ';', '<', '>' ), ' ', $allCleanString); 
	
	return $res; 
	} 

$allCleanString = "Example,to remove<the>Special'Char;"; 
$allCleanString = RemoveSpecialChar($allCleanString); 
echo $allCleanString; 
?>

You can also check other tutorial of string,

Using str_ireplace() Method:

<?php  
function RemoveSpecialChar($allCleanString){ 
	 
	$res = str_ireplace( array( '\'', '"', 
	',' , ';', '<', '>' ), ' ', $allCleanString); 
	
	return $res; 
	} 

$allCleanString = "Example,to remove<the>Special'Char;"; 
 
$allCleanString = RemoveSpecialChar($allCleanString); 
echo $allCleanString; 
?>

Using preg_replace() Method

<?php 
function RemoveSpecialChar($allCleanString){ 
	 
	$res = preg_replace('/[^a-zA-Z0-9_ -]/s',' ',$allCleanString); 
	
	return $res; 
} 
 
$allCleanString = "Example,to remove<the>Special'Char;"; 

$allCleanString = RemoveSpecialChar($allCleanString); 
echo $allCleanString; 
?>

seo friendly url using PHP

function seo_friendly_url($user_str_data){
    $user_str_data = str_replace(array('[\', \']'), '', $user_str_data);
    $user_str_data = preg_replace('/\[.*\]/U', '', $user_str_data);
    $user_str_data = preg_replace('/&(amp;)?#?[a-z0-9]+;/i', '-', $user_str_data);
    $user_str_data = htmlentities($user_str_data, ENT_COMPAT, 'utf-8');
    $user_str_data = preg_replace('/&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);/i', '\\1', $user_str_data );
    $user_str_data = preg_replace(array('/[^a-z0-9]/i', '/[-]+/') , '-', $user_str_data);
    return strtolower(trim($user_str_data, '-'));
}

I hope you get an idea about removes specials character 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.