Getting the alphabet only

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
dave_c00
Forum Commoner
Posts: 37
Joined: Wed May 28, 2003 6:08 am

Getting the alphabet only

Post by dave_c00 »

hello,

i am trying to get rid of any symbols in a string.

The only thing i want is 0-9 and a-z.

Rather than using preg_replace to remove all symbols is there a way of using regular expressions to get the correct result,

Thanks

Dave
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: Getting the alphabet only

Post by anjanesh »

dave_c00 wrote:Rather than using preg_replace to remove all symbols is there a way of using regular expressions to get the correct result,
?
preg_replace() uses Regular Expressions.
If you want to use preg_replace you need to use RegExp.
if you want to use RegExp but not preg_replace, you can use ereg_replace.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

The hard way (this is sloooooooow compared to preg_replace)

Code: Select all

$input = 'stuff you want to replace ||||';

$allowed = array(0, 1, 2, ... , 'a', 'b', .... , 'z');

$newinput = '';
$length = strlen($input);
for ($i = 0; $i < $length; ++$i)
{
  if (in_array($input{$i}, $allowed))
  {
     $newinput .= $input{$i};
  }
}
$input = $newinput;
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Any good reason why not to use preg_ ?

Code: Select all

$new_string = preg_replace('/[^a-z0-9]/i', '', $old_string);
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If he's using mbstring, there is only mb_ereg*
Post Reply