yanisdon Posted February 20, 2007 Share Posted February 20, 2007 Hi, I need some help with this replace function: Here is what I can come up with. This function looks up a string and will replace any of these characters with a space: $,@#~`%*^&()+=[-][}{;:'"<>?|\!. The expression for that works fine: $expression = "/\W{3,}|\\\{1,}/"; My problem now is, that I want to allow & What do I have to change? Cheers Jan The function: // bad characters $,@#~`%*^&()+=[-][}{;:'"<>?|\!. function nspecial($s){ $expression = "/\W{3,}|\\\{1,}/"; $s = preg_replace($expression, " ", $s); //now replace appostroph with a character of choice $s = preg_replace("/\'{1,}|\"{1,}/","`",$s); //$s = addbackslashes($s); return $s; } Quote Link to comment Share on other sites More sharing options...
fert Posted February 20, 2007 Share Posted February 20, 2007 just remove the & Quote Link to comment Share on other sites More sharing options...
yanisdon Posted February 20, 2007 Author Share Posted February 20, 2007 just remove the & // bad characters $,@#~`%*^&()+=[-][}{;:'"<>?|\!. Thats only a comment. My expression ist this: $expression = "/\W{3,}|\\\{1,}/"; How can I remove the & here? Cheers Jan Quote Link to comment Share on other sites More sharing options...
effigy Posted February 20, 2007 Share Posted February 20, 2007 \W is short for [^A-Za-z0-9_]. You can expand it to that character class, adding the &. Quote Link to comment Share on other sites More sharing options...
yanisdon Posted February 20, 2007 Author Share Posted February 20, 2007 Thank you. So you reckon it's actually a good idea to use [^A-Za-z0-9_] instead of \W ? Mind you, I want to allow the & and with \W will match all special chars. So yes, I could use [^A-Za-z0-9_] as it is and therefore & wouldn't match (Wich is exactly what I was looking for), but I was looking for a more slick way to utilise that \W expression with an exception ???? Any Ideas? Cheers Jan Quote Link to comment Share on other sites More sharing options...
effigy Posted February 21, 2007 Share Posted February 21, 2007 So you reckon it's actually a good idea to use [^A-Za-z0-9_] instead of \W ? No. What I meant was \W = [^A-Za-z0-9_]; therefore, if you want to allow ampersands use \W's expansion and add the &: [^A-Za-z0-9_&]. Quote Link to comment Share on other sites More sharing options...
yanisdon Posted February 21, 2007 Author Share Posted February 21, 2007 Well, thank you. Sorry I didn't get it. That'll do. [^A-Za-z0-9_&] What if I also would like to allow / ? Do I have to escape that because [^A-Za-z0-9_&/] doesn't seems to be working? Cheers Jan Quote Link to comment Share on other sites More sharing options...
effigy Posted February 21, 2007 Share Posted February 21, 2007 You cannot use your delimiter within the pattern without escaping it. See this post. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.