Jump to content

[SOLVED] RegEX Match all that dont match expresion


The14thGOD

Recommended Posts

More confusion to the confusing regex =P

 

I want to match all non alpha-numeric and hyphens, i read this does it:

<?php $string = preg_replace('/[^a-zA-Z0-9-]/','',$string);?>

Why does this do it? is it cause of the ^ in the brackets? I thought that was used as the starting point of the string (but I also havn't seen it inside the brackets, i think it's normally outside?).

 

So is ^ inside [] == ! ?

Thanks!

Justin

That pattern looks correct.. When dealign with ^, if it is at the very start of the pattern, this means start matching something at the beginning of the string.. but if it is the very first character in a character class like so: [^ ... ], it makes the character class negative.. so it will match (or replace in this case) any characters that are not within the class.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.