faqts : Computers : Programming : Languages : PHP : Common Problems : Regular Expressions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 7 people (71%) answered Yes
Recently 5 of 7 people (71%) answered Yes

Entry

How do I escape brackets in a regexp? I want to remove all but azAZ09_-[] from a string..

Dec 1st, 2003 14:22
Jakub Vrana, Niclas Marie,


man 7 regex says:
To  include  a  literal `]' in the list, make it the first character 
(following a possible `^').  To include  a  lit­eral `-', make it the 
first or last character.
So removing azAZ09_-[] can be made by:
<?php
$string = ereg_replace("[^]a-zA-Z0-9_[-]", "", $string);
?>