[UPHPU] Regex help.

Richard K Miller richardkmiller at gmail.com
Fri Jan 30 14:24:52 MST 2009


> Sometimes using preg_replace I get some errors, when this happends  
> you need
> to add a / at the start and the end of the regular expresion:
>
> preg_replace('/^[A-Za-z]/', '', $contents);
>

You might like this tool, which shows live highlighting of regular  
expressions:
http://www.gskinner.com/RegExr/

	Suppose the correct regexp is ^[A-Za-z]+

Once you figure out the correct regular expression, surround it with  
any delimiter, typically slashes, in addition to being quoted for the  
PHP function. Though regular expressions are typically delimited with  
slashes, you can use ampersands or commas or other symbols. (This is  
helpful if your regular expression is attempting to match slashes; if  
you use a non-slash delimiter, you won't need to escape the slashes.)

	preg_replace('/^[A-Za-z]+/', '', $contents);
		or
	preg_replace('@^[A-Za-z]+@', '', $contents);

After the closing delimiter, but before the closing quote mark, you  
can use flags such as i, s, m, or x.

	preg_replace('/^[A-Z]+/i', '', $contents); // [A-Z] becomes case  
(i)nsensitive

The flags i, s, m, and x correspond to the checkboxes ignoreCase,  
dotall, multiline, and extended, respectively, in the above tool.

Richard




More information about the UPHPU mailing list