Jump to content

preg_replace [expression] issue


yanisdon

Recommended Posts

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;

}

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.