How to Remove all Special Characters from String in PHP?

Hello Friends, today I will tell you through this article how you can remove or stript all special charecters from string through php. So let’s go.

Remove all Special Characters from String in PHP, Stript all Special Characters from String with PHP,PHP Strip all special charecters from string using php

You can remove all special charecters from any string with the function of php very easily. replace is the inbuilt function of js. You can remove all special charecters using this function. How this function will be used .Example of this is explained to you below with 2 Steps.

Step 1:- With Function Remove Special Charecters.

<?php 
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}
echo clean('a|"[email protected]£de^&$f g');
?>

Step 2:- Without Function Remove Special Charecters.

<?php
$string ='a|"[email protected]£de^&$f g';
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
echo preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
?>