Last updated on August 30th, 2022 at 04:14 pm

Non-Alpha numeric characters are any which is not A-Z,a-z or 0-9. In this tutorial we will discuss on how to remove or replace non alphanumeric characters using php. We have two examples in the demo one is removing the non alphanumeric characters and the other is replacing it with * (asterisk, you can also replace it with underscore or any other symbol of your choice)

A classic example of Non-Alphanumeric characters are !ӣ$%^&*(), Let us take a look at the script.

THE CODE

<?php
$str =  'Here! are some numbers 1244 and some non alphanumeric characters !£$%^; ';
echo "<b>Original Sring</b>: ".$str;
//replacing with just space
$new_string = preg_replace( '/[^a-zA-Z0-9\s]/ ',  ' ', $str);
echo "<br><b>Removed String</b>: ".$new_string;
//replacing with *
$new_string_1 = preg_replace( '/[^a-zA-Z0-9\s]/ ',  '*', $str);
echo "<br><b>Replaced String</b>: ".$new_string_1;
?>

This is a very simple script which remove ! and other characters from the string !£$%^&

Demo

Leave a Reply

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