Site Fuse! programming, design, hosting, promotion and advertising tips for webmasters

PHP Remove Everything but Letters and Numbers

Home  \  Forums  \  Programming & Development Forums  \  PHP Forums  \  PHP Remove Everything but Letters and Numbers
Share   

user photo

MrPhear   1 years, 6 months ago

There'are two ways to strip everything but letters and numbers from a string.

1. with preg_replace(); function

preg_replace('#\W#', '' $string);


Eg:
$string = "php__ n?othing b ut le;tte,rs and n!um.bers";
$stripped_string = preg_replace('#\W#', '' $string);
echo $stripped_string;


should output;

phpnothingbutlettersandnumbers



2. with ereg_replace(); function

ereg_replace("[^A-Za-z0-9]", "", $string );


Eg:
$string = "php__ n?othing b ut le;tte,rs and n!um.bers";
$stripped_string = ereg_replace("[^A-Za-z0-9]", "", $string );
echo $stripped_string;


should output;

phpnothingbutlettersandnumbers
user photo
Guest