hi
im new to regular expressions and would like to decipher this.
return preg_replace("/[<>]/", '_', $string);
thanks!!
hi
im new to regular expressions and would like to decipher this.
return preg_replace("/[<>]/", '_', $string);
thanks!!
It means "replace each <
or >
inside the string $string
with an underscore, then return the result".
The slashes (/
) delimit the regular expression. You can use other characters instead (preg_replace("#[<>]#", '_', $string);
would work just as well and makes sense if your regex contains a slash itself).
[]
brackets denote a character class. They mean essentially "one character of those contained within the class", so [<>]
means "either a <
or a >
".
You can also negate a character class by starting it with a ^
: [^<>]
means "any character except <
or >
.
It looks like it is replacing either the greater than or the less than sign with an underscore. But I am a little rusty in regex