<?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; regular expression</title>
	<atom:link href="http://www.web-development-blog.com/archives/tag/regular-expression/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>Parse html with PHP preg_match_all()</title>
		<link>http://www.web-development-blog.com/archives/parse-html-with-preg_match_all/</link>
		<comments>http://www.web-development-blog.com/archives/parse-html-with-preg_match_all/#comments</comments>
		<pubDate>Fri, 25 Aug 2006 22:01:22 +0000</pubDate>
		<dc:creator>Olaf</dc:creator>
				<category><![CDATA[PHP scripts]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[preg_match]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[scripts]]></category>

		<guid isPermaLink="false">http://www.web-development-blog.com/archives/parse-html-with-preg_match_all/</guid>
		<description><![CDATA[For the most of the PHP developer which are using preg_match or preg_replace frequently is the function preg_match_all a smaller advantage, but for all others it’s maybe hard to understand. The biggest difference between preg_match_all and the regular preg_match is that all matched values are stored inside a multi-dimensional array to store an unlimited number [...]]]></description>
			<content:encoded><![CDATA[<!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><p>For the most of the PHP developer which are using preg_match or <a href="http://www.php.net/preg_replace" target="_blank" rel="nofollow">preg_replace</a> frequently is the function <a href="http://www.php.net/preg_match_all" target="_blank" rel="nofollow">preg_match_all</a> a smaller advantage, but for all others it’s maybe hard to understand. The biggest difference between preg_match_all and the regular preg_match is that all matched values are stored inside a multi-dimensional array to store an unlimited number of matches. With the following example I will try to make clear how its possible to store the image paths inside a web page:<span id="more-29"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.finalwebsites.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/src=[<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span><span style="color: #0000ff;">']?([^\\&quot;'</span><span style="color: #009900;">&#93;</span>?<span style="color: #339933;">.*</span><span style="color: #009900;">&#40;</span>png<span style="color: #339933;">|</span>jpg<span style="color: #339933;">|</span>gif<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>\\<span style="color: #0000ff;">&quot;']?/i&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$images</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We take a closer look to the pattern:</p>
<p><code>"/<strong>src=[\\"']?</strong>([^\\"']?.*(png|jpg|gif))[\\"']?/i"</code></p>
<p>The first part and the last part are searching for everything that starts with src and ends with a optional quote or double quote. This could be a long string because the outer rule is very global. Next we check the rule starts within the first bracket:</p>
<p><code>"/src=[\\"']?<strong>([^\\"']?.*</strong>(png|jpg|gif))<strong>[\\"']?</strong>/i"</code></p>
<p>Now we are looking inside this long string from the outer rule for strings starting with an optional quote or double quote followed by any characters. The last part inside the inner brackets is the magic:</p>
<p><code>"/src=[\\"']?([^\\"']?.*<strong>(png|jpg|gif))</strong>[\\"']?/i"</code></p>
<p>We are looking next for a string that is followed by a file extension and match we get all the paths from the html file.</p>
<p>We need all the rules to isolate the string parts (image paths) from the rest of the html. The result looks like this (access the array $images with these indexes, or just use print_r($images)):</p>
<p><code>$images[0][0] -&gt; src="/images/english.gif"<br />
$images[1][0] -&gt; /images/english.gif<br />
$images[2][0] -&gt; gif</code></p>
<p>The index 1 is the information we need, try this example with other part of html code for a better understanding. Check our partner <a href="http://www.finalwebsites.com/">finalwebsites.com</a> for more scripts/snippets.</p>
<p><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.web-development-blog.com/archives/modifiers-in-regular-expression-patterns/" rel="bookmark" title="June 27, 2009">Modifiers in regular expression patterns</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>
<li><a href="http://www.web-development-blog.com/archives/upload-in-modal-window-and-pass-values-with-jquery/" rel="bookmark" title="September 5, 2009">Upload in modal window and pass values with jQuery</a></li>
</ul>
<p><!-- Similar Posts took 3.393 ms --></p>
<div style="clear:both;">&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://www.web-development-blog.com/archives/parse-html-with-preg_match_all/feed/</wfw:commentRss>
		<slash:comments>13</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! -->