Page 1 of 1

preg_replace certain symbols

Posted: Wed Jun 29, 2005 10:18 am
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

Posted: Wed Jun 29, 2005 10:54 am
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 = "#\*#";

Posted: Wed Jun 29, 2005 1:02 pm
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 ;)