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:  Mouseover and Mouseout Effects using Jquery Example

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,

Read Also:  Update multiple records using Laravel

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.

Read Also:  PHP Laravel carbon convert String To Date

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Get Current login Auth user details using Laravel 5.7
  2. Read Also:  Nested select sql multiple subqueries
  3. Read Also:  MySQL Remove special characters from database
  4. Read Also:  PHP String Cleanup Functions with Example
  5. Read Also:  Remove Special characters from String php
  6. Read Also:  PHP Rest API And HTTP CURL methods GET, POST, DELETE, PUT
  7. Read Also:  How to select only one checkbox at a time in javascript example?
  8. Read Also:  PHP Login logout example with session
  9. Read Also:  How To Solve The Page Expired 419 Error In Laravel?
  10. Read Also:  How to Send Push notification using FCM using php
CLOSEX