Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Tue Dec 18, 2007 2:26 am
I want the result to be
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
s.dot
Tranquility In Moderation
Posts: 5001 Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana
Post
by s.dot » Tue Dec 18, 2007 2:48 am
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.
GeertDD
Forum Contributor
Posts: 274 Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium
Post
by GeertDD » Tue Dec 18, 2007 6:58 am
Scrumpy.Gums
Forum Commoner
Posts: 71 Joined: Thu Aug 30, 2007 2:57 pm
Location: Bristol, UK
Post
by Scrumpy.Gums » Tue Dec 18, 2007 7:09 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Dec 18, 2007 7:38 am
similar to this should be sufficient
Code: Select all
$pattern = array('~\W+~', '~\s+');
$replace = array('', '-');
$final = preg_replace($pattern, $replace, trim($input));
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Tue Dec 18, 2007 8:40 am
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 .
s.dot
Tranquility In Moderation
Posts: 5001 Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana
Post
by s.dot » Tue Dec 18, 2007 2:21 pm
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.
stereofrog
Forum Contributor
Posts: 386 Joined: Mon Dec 04, 2006 6:10 am
Post
by stereofrog » Tue Dec 18, 2007 6:49 pm
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]?