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
Getting the alphabet only
Moderator: General Moderators
Re: Getting the alphabet only
?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.
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;- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Any good reason why not to use preg_ ?
Code: Select all
$new_string = preg_replace('/[^a-z0-9]/i', '', $old_string);