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
https://forums.phpfreaks.com/topic/39245-preg_replace-expression-issue/
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

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.