<?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"
	>

<channel>
	<title>Website Design Blog by DreamCo Design &#187; PHP</title>
	<atom:link href="http://www.dreamcodesign.com/blog/category/web-programming/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dreamcodesign.com/blog</link>
	<description>Web Design 101</description>
	<pubDate>Mon, 24 Nov 2008 19:05:55 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>Regular Expressions in PHP</title>
		<link>http://www.dreamcodesign.com/blog/8/</link>
		<comments>http://www.dreamcodesign.com/blog/8/#comments</comments>
		<pubDate>Wed, 07 Mar 2007 19:10:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Web Programming]]></category>

		<guid isPermaLink="false">http://www.dreamcodesign.com/blog/?p=9</guid>
		<description><![CDATA[ 
Regular Expressions Tutorial
 
It&#8217;s very hard to find one complete source for information on the internet to learn about regular expressions in PHP.  It&#8217;s one of the most intimidating things when coding using PHP, especially when you&#8217;re still trying to learn about it.  The collection below is all of the information I [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><strong><span>Regular Expressions Tutorial<o :p></o></span></strong></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span>It&#8217;s very hard to find one complete source for information on the internet to learn about regular expressions in PHP.</span><span>  </span>It&#8217;s one of the most intimidating things when coding using PHP, especially when you&#8217;re still trying to learn about it.<span>  </span>The collection below is all of the information I could collect on and about regular expressions in PHP and I figured it would be a good idea to post them here so others didn&#8217;t have the same problems as I have had in the past.<span>  </span>I have included links to the websites I pulled the information from, and if I didn&#8217;t then I got it from the main PHP.net website.<o :p></o></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span>Enjoy!</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span id="more-9"></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><strong><span>Basic Syntax of Regular Expressions (as from PHPBuilder.com)<o :p></o></span></strong></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>First of all, let&#8217;s take a look at two special symbols: &#8216;^&#8217; and &#8216;$&#8217;. What they do is indicate the<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>start and the end of a string, respectively, like this:<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;^The&#8221;: matches any string that starts with &#8220;The&#8221;;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;of despair$&#8221;: matches a string that ends in the substring &#8220;of despair&#8221;;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;^abc$&#8221;: a string that starts and ends with &#8220;abc&#8221; &#8212; that could only be &#8220;abc&#8221; itself!<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;notice&#8221;: a string that has the text &#8220;notice&#8221; in it.<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>You can see that if you don&#8217;t use either of the two characters we mentioned, as in the last example,<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>you&#8217;re saying that the pattern may occur anywhere inside the string &#8212; you&#8217;re not &#8220;hooking&#8221; it to any of the edges.<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>There are also the symbols &#8216;*&#8217;, &#8216;+&#8217;, and &#8216;?&#8217;, which denote the number of times a character or a sequence of<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>characters may occur. What they mean is: &#8220;zero or more&#8221;, &#8220;one or more&#8221;, and &#8220;zero or one.&#8221; Here are some examples:<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;ab*&#8221;: matches a string that has an a followed by zero or more b&#8217;s (&#8221;a&#8221;, &#8220;ab&#8221;, &#8220;abbb&#8221;, etc.);<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;ab+&#8221;: same, but there&#8217;s at least one b (&#8221;ab&#8221;, &#8220;abbb&#8221;, etc.);<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;ab?&#8221;: there might be a b or not;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;a?b+$&#8221;: a possible a followed by one or more b&#8217;s ending a string.<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>You can also use bounds, which come inside braces and indicate ranges in the number of occurences:<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;ab{2}&#8221;: matches a string that has an a followed by exactly two b&#8217;s (&#8221;abb&#8221;);<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;ab{2,}&#8221;: there are at least two b&#8217;s (&#8221;abb&#8221;, &#8220;abbbb&#8221;, etc.);<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;ab{3,5}&#8221;: from three to five b&#8217;s (&#8221;abbb&#8221;, &#8220;abbbb&#8221;, or &#8220;abbbbb&#8221;).<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>Note that you must always specify the first number of a range (i.e, &#8220;{0,2}&#8221;, not &#8220;{,2}&#8221;). Also, as you might<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>have noticed, the symbols &#8216;*&#8217;, &#8216;+&#8217;, and &#8216;?&#8217; have the same effect as using the bounds &#8220;{0,}&#8221;, &#8220;{1,}&#8221;, and &#8220;{0,1}&#8221;,<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>respectively.<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><br />
<script type="text/javascript"> <!-- google_ad_client = "pub-2951450649862551"; google_ad_width = 300; google_ad_height = 250; google_ad_format = "300x250_as"; google_ad_type = "text_image"; google_ad_channel =""; google_color_border = "666666"; google_color_bg = "666666"; google_color_link = "FFFFFF"; google_color_url = "FFFFFF"; google_color_text = "FFFFFF"; //--> </script><o :p> </o></p>
<p></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script><o :p> </o></p>
<p></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>Now, to quantify a sequence of characters, put them inside parentheses:<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;a(bc)*&#8221;: matches a string that has an a followed by zero or more copies of the sequence &#8220;bc&#8221;;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;a(bc){1,5}&#8221;: one through five copies of &#8220;bc.&#8221;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>There&#8217;s also the &#8216;|&#8217; symbol, which works as an OR operator:<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;hi|hello&#8221;: matches a string that has either &#8220;hi&#8221; or &#8220;hello&#8221; in it;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;(b|cd)ef&#8221;: a string that has either &#8220;bef&#8221; or &#8220;cdef&#8221;;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;(a|b)*c&#8221;: a string that has a sequence of alternating a&#8217;s and b&#8217;s ending in a c;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>A period (&#8217;.') stands for any single character:<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;a.[0-9]&#8220;: matches a string that has an a followed by one character and a digit;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;^.{3}$&#8221;: a string with exactly 3 characters.<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>Bracket expressions specify which characters are allowed in a single position of a string:<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;[ab]&#8220;: matches a string that has either an a or a b (that&#8217;s the same as &#8220;a|b&#8221;);<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;[a-d]&#8220;: a string that has lowercase letters &#8216;a&#8217; through &#8216;d&#8217; (that&#8217;s equal to &#8220;a|b|c|d&#8221; and even &#8220;[abcd]&#8220;);<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;^[a-zA-Z]&#8220;: a string that starts with a letter;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;[0-9]%&#8221;: a string that has a single digit before a percent sign;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;,[a-zA-Z0-9]$&#8221;: a string that ends in a comma followed by an alphanumeric character.<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>You can also list which characters you DON&#8217;T want &#8212; just use a &#8216;^&#8217; as the first symbol in a bracket expression<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>(i.e., &#8220;%[^a-zA-Z]%&#8221; matches a string with a character that is not a letter between two percent signs).<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>In order to be taken literally, you must escape the characters &#8220;^.[$()|*+?{\" with a backslash ('\'), as<br />
<!--[if !supportLineBreakNewLine]&#8211;><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>they have special meaning. On top of that, you must escape the backslash character itself in PHP3 strings, so,<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>for instance, the regular expression &#8220;(\$|¥)[0-9]+&#8221; would have the function call: ereg(&#8221;(\\$|¥)[0-9]+&#8221;, $str)<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>(what string does that validate?)<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><strong><span>Example 1.</span></strong><span> Examples of valid patterns<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>* /&lt;\/\w+&gt;/<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>* |(\d{3})-\d+|Sm<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>* /^(?i)php[34]/<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>* {^\s+(\s+)?$}<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><strong><span>Example 2.</span></strong><span> Examples of invalid patterns<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>* /href=&#8217;(.*)&#8217; - missing ending delimiter<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>* /\w+\s*\w+/J - unknown modifier &#8216;J&#8217;<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>* 1-\d3-\d3-\d4| - missing starting delimiter<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><strong><span>Some useful PHP Keywords and their use<o :p></o></span></strong></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><strong><em><span>preg_split</span></em></strong><strong><span><o :p></o></span></strong></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>(PHP 3&gt;= 3.0.9, PHP 4 )<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>preg_split &#8212; Split string by a regular expression<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>Description<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>array preg_split ( string pattern, string subject [, int limit [, int flags]])<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>Returns an array containing substrings of subject split along boundaries matched by pattern.<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>If limit is specified, then only substrings up to limit are returned, and if limit is -1, it<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>actually means &#8220;no limit&#8221;, which is useful for specifying the flags.<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>flags can be any combination of the following flags (combined with bitwise | operator):<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>PREG_SPLIT_NO_EMPTY<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>If this flag is set, only non-empty pieces will be returned by preg_split().<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>PREG_SPLIT_DELIM_CAPTURE<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>If this flag is set, parenthesized expression in the delimiter pattern will be captured and<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>returned as well. This flag was added for 4.0.5.<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>PREG_SPLIT_OFFSET_CAPTURE<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>If this flag is set, for every occuring match the appendant string offset will also be<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>returned. Note that this changes the return value in an array where every element is an<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>array consisting of the matched string at offset 0 and it&#8217;s string offset into subject<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>at offset 1. This flag is available since PHP 4.3.0 .<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><strong><span>Example 1. </span></strong><span>preg_split() example : Get the parts of a search string<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&lt;?php<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>// split the phrase by any number of commas or space characters,<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>// which include &#8221; &#8220;, \r, \t, \n and \f<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>$keywords = preg_split (&#8221;/[\s,]+/&#8221;, &#8220;hypertext language, programming&#8221;);<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>?&gt;<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><strong><span>Example 2. </span></strong><span>Splitting a string into component characters<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&lt;?php<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>$str = &#8217;string&#8217;;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>$chars = preg_split(&#8217;//&#8217;, $str, -1, PREG_SPLIT_NO_EMPTY);<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>print_r($chars);<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>?&gt;<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><strong><span>Example 3. </span></strong><span>Splitting a string into matches and their offsets<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&lt;?php<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>$str = &#8216;hypertext language programming&#8217;;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>$chars = preg_split(&#8217;/ /&#8217;, $str, -1, PREG_SPLIT_OFFSET_CAPTURE);<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>print_r($chars);<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>?&gt;<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>will yield:<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>Array<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>(<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>[0] =&gt; Array<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>(<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>[0] =&gt; hypertext<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>[1] =&gt; 0<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>)<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>[1] =&gt; Array<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>(<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>[0] =&gt; language<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>[1] =&gt; 10<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>)<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>[2] =&gt; Array<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>(<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>[0] =&gt; programming<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>[1] =&gt; 19<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>)<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>)<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><em><span>Note: Parameter flags was added in PHP 4 Beta 3.</span></em><span><o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><strong><em><span>preg_match</span></em></strong><strong><span><o :p></o></span></strong></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>(PHP 3&gt;= 3.0.9, PHP 4 )<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>preg_match &#8212; Perform a regular expression match<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>Description<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>int preg_match ( string pattern, string subject [, array matches [, int flags]])<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>Searches subject for a match to the regular expression given in pattern.<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>If matches is provided, then it is filled with the results of search. $matches[0] will<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>contain the text that matched the full pattern, $matches[1] will have the text that matched<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>the first captured parenthesized subpattern, and so on.<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>flags can be the following flag:<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>PREG_OFFSET_CAPTURE<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>If this flag is set, for every occuring match the appendant string offset will also<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>be returned. Note that this changes the return value in an array where every element<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>is an array consisting of the matched string at offset 0 and it&#8217;s string offset into<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>subject at offset 1. This flag is available since PHP 4.3.0 .<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>The flags parameter is available since PHP 4.3.0 .<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><em><span>preg_match()</span></em><span> returns the number of times pattern matches. That will be either 0 times<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>(no match) or 1 time because preg_match() will stop searching after the first match.<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><em><span>preg_match_all()</span></em><span> on the contrary will continue until it reaches the end of subject.<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><em><span>preg_match()</span></em><span> returns FALSE if an error occured.<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>Tip: Do not use preg_match() if you only want to check if one string is contained<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>in another string. Use strpos() or strstr() instead as they will be faster.<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><strong><span>Example 1.</span></strong><span> Find the string of text &#8220;php&#8221;<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&lt;?php<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>// The &#8220;i&#8221; after the pattern delimiter indicates a case-insensitive search<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>if (preg_match (&#8221;/php/i&#8221;, &#8220;PHP is the web scripting language of choice.&#8221;)) {<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>print &#8220;A match was found.&#8221;;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>} else {<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>print &#8220;A match was not found.&#8221;;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>}<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>?&gt;<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&lt;strong&gt;Example 2.&lt;/strong&gt; Find the word &#8220;web&#8221;<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&lt;?php<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>/* The \b in the pattern indicates a word boundary, so only the distinct<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>* word &#8220;web&#8221; is matched, and not a word partial like &#8220;webbing&#8221; or &#8220;cobweb&#8221; */<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>if (preg_match (&#8221;/\bweb\b/i&#8221;, &#8220;PHP is the web scripting language of choice.&#8221;)) {<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>print &#8220;A match was found.&#8221;;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>} else {<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>print &#8220;A match was not found.&#8221;;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>}<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>if (preg_match (&#8221;/\bweb\b/i&#8221;, &#8220;PHP is the website scripting language of choice.&#8221;)) {<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>print &#8220;A match was found.&#8221;;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>} else {<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>print &#8220;A match was not found.&#8221;;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>}<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>?&gt;<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&lt;strong&gt;Example 3.&lt;/strong&gt; Getting the domain name out of a URL<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&lt;?php<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>// get host name from URL<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>preg_match(&#8221;/^(http:\/\/)?([^\/]+)/i&#8221;,<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>&#8220;http://www.php.net/index.html&#8221;, $matches);<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>$host = $matches[2];<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>// get last two segments of host name<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>preg_match(&#8221;/[^\.\/]+\.[^\.\/]+$/&#8221;, $host, $matches);<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>echo &#8220;domain name is: {$matches[0]}\n&#8221;;<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o :p></o></span>
</p>
<p class="MsoNormal" style="line-height: normal"><span>?&gt;<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>This example will produce:<o :p></o></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span><o :p> </o></span></p>
<p class="MsoNormal" style="line-height: normal"><span>domain name is: php.net<o :p></o></span></p>
<p class="MsoNormal"><o :p> </o></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dreamcodesign.com/blog/8/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
