Page 1 of 1

Getting the alphabet only

Posted: Tue Aug 02, 2005 7:13 am
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

Re: Getting the alphabet only

Posted: Tue Aug 02, 2005 7:34 am
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.

Posted: Tue Aug 02, 2005 5:21 pm
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;

Posted: Tue Aug 02, 2005 6:00 pm
by Chris Corbyn
Any good reason why not to use preg_ ?

Code: Select all

$new_string = preg_replace('/[^a-z0-9]/i', '', $old_string);

Posted: Tue Aug 02, 2005 6:06 pm
by timvw
If he's using mbstring, there is only mb_ereg*