Search code examples
phpsyntax-errorparse-error

How to use ' in a single quoted string?


I use the following for a preg_replace:

$replace = '/[!\/."",#\s\-:?"]+/';

For example when I try and add ' above it gives me an error on the file:

Parse error: syntax error, unexpected T_NS_SEPARATOR in .../s.php on line 38

Additionally, is there an easier way to list all of the symbols to be replaced?


Solution

  • If you want to get rid of any symbol, try:

    $replace = "/[^a-zA-Z0-9 ]/";
    

    The ^ indicates "anything but the following". Add whatever characters you want to allow to the list. It's easier than listing every symbol and having to deal with escaping.