Page 1 of 1

All "non-word" characters except whitespace

Posted: Tue Dec 18, 2007 2:26 am
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

Posted: Tue Dec 18, 2007 2:48 am
by s.dot
probably along the lines of

[[^\s]\W]

Posted: Tue Dec 18, 2007 6:58 am
by GeertDD
Having a look at this function may help:
http://trac.kohanaphp.com/browser/trunk ... v=1552#L97

Posted: Tue Dec 18, 2007 7:09 am
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.

Posted: Tue Dec 18, 2007 7:38 am
by feyd
similar to this should be sufficient

Code: Select all

$pattern = array('~\W+~', '~\s+');
$replace = array('', '-');
$final = preg_replace($pattern, $replace, trim($input));

Posted: Tue Dec 18, 2007 8:40 am
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.

Posted: Tue Dec 18, 2007 2:21 pm
by s.dot
Just out of curiosity, are you trying to do something like this?

Posted: Tue Dec 18, 2007 6:49 pm
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]?