/    Sign up×
Community /Pin to ProfileBookmark

Need help with a complex regular expression – how to ONLY allow characters and number

I’ve got is a system where users can duplicate directories

I want the user to be only allowed to enter characters and numbers when choosing a new name for the duplicated directory

ALL other characters should be left out AND white space characters need to be replaced with a single ‘-‘ (no matter how many of them)

I’ve got this to replace white space characters:

$str = preg_replace(‘%s+%’,’-‘, $str);

But I’m a little clueless as to how to replace ANY other character that isn’t a character or number

ANY help would be appreciated

Thanks

OM

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiOct 14.2009 — [code=php]
$map = array(
'/s+/' => '-',
'/[^a-z0-9-]+/i' => ''
);
$string = preg_replace(array_keys($map), $map, trim($string));
[/code]
Copy linkTweet thisAlerts:
@OM2authorOct 14.2009 — thank u ?
×

Success!

Help @OM2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...