preg_replace certain symbols

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
dave_c00
Forum Commoner
Posts: 37
Joined: Wed May 28, 2003 6:08 am

preg_replace certain symbols

Post by dave_c00 »

Hello,

I am desperately trying to write a preg_replace function to strip the symbols;


* ^ & ' " ( ) % $ £


out of a string

Can anyone help?

Thanks

Dave
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

an entire forum dedicated to regex we have. Posted in there this should have been.

reserved are most of those symbols, so escape them you must using "\".

ex:

Code: Select all

$pattern = "#\*#";
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Like Burr says... just escape them since most of them are reserved. Single characters *might* be easier to do with str_replace()

Code: Select all

$bad = array (
    '/\\*/',
    '/\\^/',
    '/&/',
    '/\\\'/',
    '/\\"/',
    '/\\(/',
    '/\\)/',
    '/\\%/',
    '/\\$/',
    '/£/'
);

$new_string = preg_replace($bad, '', $string);
Moved to regex ;)
Post Reply