All "non-word" characters except whitespace

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

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

All "non-word" characters except whitespace

Post by anjanesh »

Code: Select all

$a = 'jon!at"`ha""n smith';
I want the result to be

Code: Select all

jonathan-smith

Code: Select all

$a = preg_replace('#\W#', '', $a);
removes whitespace too.
Both of these dont work

Code: Select all

$a = preg_replace(array('#\s#', '#\W#'), array('-', ''), $a);
$a = preg_replace('#[\W & ^\s]#', '', $a);
Is there a way using \W ? I dont want t define all the non-Word characters using [].

All \W except \s

Thanks
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

probably along the lines of

[[^\s]\W]
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

Having a look at this function may help:
http://trac.kohanaphp.com/browser/trunk ... v=1552#L97
Scrumpy.Gums
Forum Commoner
Posts: 71
Joined: Thu Aug 30, 2007 2:57 pm
Location: Bristol, UK

Post by Scrumpy.Gums »

Would it not be easier to match what you want rather than remove what you don't want? Something along the lines of:

Code: Select all

preg_match_all("#[\w ]#", $a, $result);

echo implode("", $result[0]);
In your example this returns:

jonathan smith

you could then use a simple str_replace to replace the whitespace.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

similar to this should be sufficient

Code: Select all

$pattern = array('~\W+~', '~\s+');
$replace = array('', '-');
$final = preg_replace($pattern, $replace, trim($input));
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Problem is, \W includes \s too.

EDIT:

Code: Select all

<?php
$a = 'Jon!at"`ha""n Smith';
$a = preg_replace(array('#[^a-zA-Z0-9\s]#', '#\s#'), array('', '-'), $a);
echo $a;
?>
Thjis seem to work, but Im wondering how to get it done with \W but not \s.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Just out of curiosity, are you trying to do something like this?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

anjanesh wrote:Problem is, \W includes \s too.

EDIT:

Code: Select all

<?php
$a = 'Jon!at"`ha""n Smith';
$a = preg_replace(array('#[^a-zA-Z0-9\s]#', '#\s#'), array('', '-'), $a);
echo $a;
?>
Thjis seem to work, but Im wondering how to get it done with \W but not \s.
What's wrong with [^\w\s]?
Post Reply