encode a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
revbackup
Forum Commoner
Posts: 29
Joined: Tue Jun 09, 2009 1:52 am

encode a string

Post 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...
User avatar
genconv
Forum Commoner
Posts: 34
Joined: Sun Jul 05, 2009 9:27 am

Re: encode a string

Post 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);
}
 
Post Reply