31

I'm trying to strip all punctuation out of a string using a simple regular expression and the php preg_replace function, although I get the following error:

Compilation failed: POSIX named classes are supported only within a class at offset 0

I guess this means I can't use POSIX named classes outside of a class at offset 0. My question is, what does it means when it says "within a class at offset 0 "?

$string = "I like: perl";    

if (eregi('[[:punct:]]', $string))  
    $new = preg_replace('[[:punct:]]', ' ', $string); echo $new;
3
  • Sorry about the lack of tags, I was getting an error when tying to tag this question. "You can't add new tags, please use existing tags".. How am I supposed to know what that means? Commented Jan 24, 2009 at 0:14
  • I fixed the tags. I think there's a bug in your program: did you mean for the echo to occur outside or inside of the if block? Commented Jan 24, 2009 at 0:17
  • I think you need some level of reputation to create a new tag. But the error message is poor. Maybe you could suggest a change to the UserVoice feedback site listed below. VVVVVV Commented Jan 24, 2009 at 0:19

4 Answers 4

47

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

preg_replace('/[[:punct:]]/', ' ', $string)
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic, not only does this solve my problem, but I also learnt something new about how preg_* works. Thanks :)
5

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.

8 Comments

I'm pretty new to the complexities of regular expressions, what "specifically" is the definition of 'g'? I'd assume from you've said that it's a way of telling the regexp to be recursive?
@chris: It means "Global" -- i.e., find all existing matches. Without it, regex functions will stop searching after the first match.
@Lonowski, thanks; I have updated my answer with your information.
/g means global, which tells regex to replace all instances. echo "What's up?"|sed "s/[[:punct:]]/ /" What s up? echo "What's up?"|sed "s/[[:punct:]]/ /g" What s up However, strager's wrong. preg_replace is /default/ global. echo preg_replace('/[[:punct:]]/', ' ', "What's up?"); What s up
In this aspect the PHP implementation is not really following the Perl standard. The preg_replace() function always replaces all matches.
|
1

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.)

3 Comments

Is there an alternative to ereg_replace for what I'm doing?
@chris, He said ereg. You're using preg_replace. You're also using eregi (which isn't necessary, really, in this situation) which is what @Ant was talking about.
Sorry, moment of zero concentrtion. I see what he's saying now.
0

I just added g to the regexp as suggested in one of the anwers, it did the opposite of wahts expected and DIDN'T filter out the punctuation, turns out preg_replace doesnt require g as it's global/recursive in the first place

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.