Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP regex: what is "class at offset 0"?

Tags:

regex

php

The preg_* functions expect Perl compatible regular expressions with delimiters. So try this:

preg_replace('/[[:punct:]]/', ' ', $string)

NOTE: The g modifier is not needed with PHP's PCRE implementation!

In addition to Gumbo's answer, use the g modifier to replace all occurances of punctuation:

preg_replace('/[[:punct:]]/g', ' ', $string)
//                         ^

From Johnathan Lonowski (see comments):

> [The g modifier] means "Global" -- i.e., find all existing matches. Without it, regex functions will stop searching after the first match.

An explanation of why you're getting that error: PCRE uses Perl's loose definition of what a delimiter is. Your outer []s look like valid delimiters to it, causing it to read [:punct:] as the regex part.

(Oh, and avoid the ereg functions if you can - they're not going to be included in PHP 5.3.)