Page 1 of 1

encode a string

Posted: Tue Jul 07, 2009 12:59 am
by revbackup
Hi,

can you give me a php function that will convert
a given string into letters and numbers only?
not including symbols..?

I have tried base6_encode but it has + and / included when
I encode the string...
All I want is only numbers and letters...

Please reply if you know any....
thanks...

Re: encode a string

Posted: Tue Jul 07, 2009 1:18 am
by genconv
You should use regular expressions:

Code: Select all

 
<?php
$string = 'asdasf we*(^789shfjkasdhfh&*%56 95';
 
//with white spaces
$pattern = '/[^a-zA-Z0-9\s]/u';
echo preg_replace($pattern, '', (string) $string);
 
//only numbers and letters
$pattern = '/[^a-zA-Z0-9]/u';
echo preg_replace($pattern, '', (string) $string);
 
You can also create a very simple function:

Code: Select all

 
function alnumFilter($string){
    $pattern = '/[^a-zA-Z0-9]/u';
    return preg_replace($pattern, '', (string) $string);
}