Text Size
Sunday, May 20, 2012
Get a look down
General >> Code snippets >> How to strip non alphanumeric characters from a defined string with PHP
Wednesday, 08 February 2012 11:05

How to strip non alphanumeric characters from a defined string with PHP

Written by Nabil Sadki
Rate this item
(1 vote)

Here's a little snippet that allow's us to remove any non alphanumeric characters from strings. It would be very usefull when a developper need to format a given string from a registration form for example.

1
2
3
4
5
6
7
8
9
10
<?php
// Our string
$words = "Hello! My Name Is Nabil SADKI 007!!!";
 
// Formatted string
$formatted_words = preg_replace("[^A-Za-z0-9]", "", $words ); // This will print : HelloMyNameIsNabilSADKI007
 
// Get only alpha
$formatted_words = preg_replace("[^A-Za-z]", "", $words ); // This will print : HelloMyNameIsNabilSADKI
?>
Last modified on Wednesday, 08 February 2012 11:18
Nabil Sadki

Nabil Sadki

Hello, I am Nabyl, a Freelance web developer and a joomla addict. I have made many sites using the latter, but also patches for the core of this CMS. I like HipHop culture and world of warcraft. You can find find me on Twitter when i'm free...

Website: www.coins.ma E-mail: This e-mail address is being protected from spambots. You need JavaScript enabled to view it

Add comment



Usefull tricks