phpregexpreg-replacemetacharacters

What does \W+ mean in a regular expression?


I have found this:

$text = preg_replace('/\W+/', '-', $text);

Can anyone tell me what exactly this does? There is no information about what /\W+/ means.


Solution

  • \W means a non-alphanumeric character, so anything other than a-z, A-Z, 0-9, or underscore.

    This is standard for regular expressions, nothing specific to Php.

    Here's a great tool for testing regular expressions:

    http://www.gskinner.com/RegExr/

    If you put \W+ in the box at the top you'll see what kinds of things it matches.

    PS: Here's another tool that's simpler and cleaner, though perhaps not as feature rich:

    http://rubular.com/

    It includes a handy quick-reference for regular expressions at the bottom.