Hex range in reg. exp.

I want to do this:

$text = preg_replace("/[x00-x1f]/", "", $text);

In order to strip some non-printing characters from a content source outside my control.

But that breaks the parsing of the regexp for some reason. Strange. My server says:

Warning: preg_replace() [function.preg-replace]: No ending delimiter '/' found in /home/sf/projects/swl/www/ng/index.php on line 98

Any other reg exp inside that same call to preg_replace works fine. Maybe you’ve seen this before? I will be ever grateful for any ideas or leads.

Thanks

Could you tell us what other regexes you’ve tried? Apparently there’s something syntactically wrong with this one.

Hi Zyppora

This works:

$text = preg_replace("/[a-f]/", "", $text);

But this doesn’t:

$text = preg_replace("/x00/", "", $text);

So I guess the error is with representing a character by hex value. Every documentation I’ve found for Perl style regexp says it’s xnn, where nn is the hex value. Is there a different syntax in PHP?

I’m starting to suspect that there is data corruption in my input stream that is messing up the parsing of preg_replace. It is not the only function that is behaving in a different way than described on php.net when trying to manipulate this text.

Is that possible? If the data coming in has messed up chunks that don’t get neatly recognized as characters, but are still present, could the PHP string and regexp functions fail or behave bizzarely?

Try using a different delimiter, such as @. Will the same error pop up?

Hello swafran, here you go:

$text = preg_replace("/[\x00-\x1f]/", "", $text);

thanks lordfrikk, that works like a charm

seems like the backslash has to be escaped to pass through as a string argument to preg_replace. Yeesh, that makes sense but is conterintuitive to someone like me who codes in JavaScript all the time and is used to passing actual regexp objects, not string representations thereof

Thanks a bunch!

:D
Sponsor our Newsletter | Privacy Policy | Terms of Service