Strip characters from a single directory name

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

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Strip characters from a single directory name

Post 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???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)));
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Strip characters from a single directory name

Post by GeertDD »

Hockey wrote:

Code: Select all

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