<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web Development Blog &#187; pcre</title>
	<atom:link href="http://www.web-development-blog.com/archives/tag/pcre/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.web-development-blog.com</link>
	<description>Web development tutorials, SEO articles and PHP script resources</description>
	<lastBuildDate>Sun, 25 Jul 2010 14:38:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Modifiers in regular expression patterns</title>
		<link>http://www.web-development-blog.com/archives/modifiers-in-regular-expression-patterns/</link>
		<comments>http://www.web-development-blog.com/archives/modifiers-in-regular-expression-patterns/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 11:12:40 +0000</pubDate>
		<dc:creator>Olaf</dc:creator>
				<category><![CDATA[PHP scripts]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[match]]></category>
		<category><![CDATA[modifiers]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[pcre]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.web-development-blog.com/?p=309</guid>
		<description><![CDATA[We all like regex patterns, because it&#8217;s a great way to select, filter or replace strings, numbers or complete code blocks. How about PCRE modifiers, do you use them very often? A few weeks ago I need to use one because I get in trouble with line breaks in some string, I needed to select [...]]]></description>
			<content:encoded><![CDATA[<!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><p>We all like regex patterns, because it&#8217;s a great way to select, filter or replace strings, numbers or complete code blocks. How about PCRE modifiers, do you use them very often? A few weeks ago I need to use one because I get in trouble with line breaks in some string, I needed to select in some transaction. I will show in this article some examples with the &#8220;most important&#8221; PCRE modifiers.</p>
<blockquote><p>Note each modifier is responsible for the whole pattern!</p></blockquote>
<h3>Upper and lowercase letters</h3>
<p>Using and &#8220;i&#8221; will match both upper and lowercase letters. Use <code>'/[a-z]*/i'</code> to match those strings.</p>
<h3>Match patterns across multiple lines</h3>
<p>Normally a regex test ends at end of each line, using the &#8220;m&#8221; modifier the string is processed until the end is reached, example:<span id="more-309"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Hello 
World'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^World$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Yes!'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this will not work</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^World$/m'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Yes, a multiline!'</span><span style="color: #339933;">;</span></pre></div></div>

<p>The first test will stop after &#8220;Hello&#8221; word and the second test will find the word &#8220;World&#8221; on the second row. </p>
<h3>Match all characters even new line characters</h3>
<p>Some times the multi line modifier is not useful, That&#8217;s the moment that the modifier &#8220;s&#8221; will help. Using this modifier your &#8220;.&#8221; (dot) pattern will match every character including new lines.</p>
<h3>Ignore whitespace!</h3>
<p>If you need to select some string within a particular HTML tag it might be useful to ignore whitespace. Using the modifier &#8220;x&#8221; all whitespace is ignored. If you insert comments into complicated patterns, this modifier will ignore even whitespace in those comments.</p>
<h3>Switch to &#8220;Ungreedy&#8221;</h3>
<p>Normally your engine will test against the pattern until the end of the string and will result into true/not true. This is painful if you need to collect characters in (sub)classes. This example is about to use a regular expression to collect one or more <strong>href</strong> attributes from a regular web page. This could be very difficult if there are more than one link elements. The modifier &#8220;U&#8221; makes your pattern &#8220;ungreedy&#8221;.</p>
<p>There are many more modifiers mentioned in the <a href="http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php" rel="nofollow">PHP manual</a>. Check this link to get more information. Note modifiers are cool options to optimize or extend your pattern, they are never a base functionality!</p>
<p>Check this <a href="http://www.finalwebsites.com/snippets.php?id=33">regular expression</a> example page with a few useful regex patterns.<br />
<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.web-development-blog.com/archives/parse-html-with-preg_match_all/" rel="bookmark" title="August 25, 2006">Parse html with PHP preg_match_all()</a></li>
<li><a href="http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/" rel="bookmark" title="September 1, 2009">Sending e-mails via SMTP with PHPmailer and Gmail</a></li>
<li><a href="http://www.web-development-blog.com/archives/upload-images-for-usage-in-tinymce/" rel="bookmark" title="September 25, 2008">Upload images for usage in TinyMCE</a></li>
</ul>
<p><!-- Similar Posts took 3.405 ms --></p>
<div style="clear:both;">&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://www.web-development-blog.com/archives/modifiers-in-regular-expression-patterns/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->