Page 1 of 1

Strip characters from a single directory name

Posted: Mon Dec 17, 2007 3:39 pm
by alex.barylski
I'm currently using this:

Code: Select all

$layout = preg_replace('[^0-9A-Za-z]', '', $layout);
After glancing over CC's regex tutorial for a refresher course I have re-written it as this:

Code: Select all

$layout = preg_replace('/^[\w]$/', '', $layout);
Is the bottom more accurate to what I expect?

Basically remove ANY character that is not A-Z0-9_ from the *entire* string???

Posted: Mon Dec 17, 2007 3:59 pm
by feyd
You can test what it will remove by passing it a full bank of ASCII characters:

Code: Select all

$chars = implode('',array_map('chr',range(0,255)));

Re: Strip characters from a single directory name

Posted: Tue Dec 18, 2007 6:55 am
by GeertDD
Hockey wrote:

Code: Select all

$layout = preg_replace('/^[\w]$/', '', $layout);
Further rewrite that to use \W which is the same as [^\w].