<?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>Webmaster-Source &#187; Regular Expressions</title>
	<atom:link href="https://www.webmaster-source.com/tag/regular-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.webmaster-source.com</link>
	<description>Useful Resources For Webmasters</description>
	<lastBuildDate>Thu, 24 Aug 2017 02:01:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Regular Expression to Get All Image URLs From a Page</title>
		<link>https://www.webmaster-source.com/2009/07/30/regular-expression-to-get-all-image-urls-from-a-page/</link>
		<comments>https://www.webmaster-source.com/2009/07/30/regular-expression-to-get-all-image-urls-from-a-page/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 11:59:22 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=2408</guid>
		<description><![CDATA[Have you ever wanted to, while working on some sort of PHP project, get an array listing of all the images used in a chunk of HTML? I&#8217;ve been planning out a web app over the past couple months, which will be doing a bit of RSS parsing, and I thought it would be nice [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Have you ever wanted to, while working on some sort of PHP project, get an array listing of all the images used in a chunk of HTML? I&#8217;ve been planning out a web app over the past couple months, which will be doing a bit of RSS parsing, and I thought it would be nice to do just that, when it came time to start coding. Suppose you were going to show a summary of an article from a feed, with a link to the original source. Wouldn&#8217;t it look better if you pulled an image from that article, scaled it down if necessary, and displayed it next to it? (Caching it, of course. Hotlinking == bad.)</p>
<p>I was reading an article from Cats Who Code and, lo and behold, there was a code snippet that did just that with a regular expression. (I decided to file it away to save time in the future.)</p>
<pre class="brush: php; title: ; notranslate">
$images = array();
preg_match_all('/(img|src)=(&quot;|')[^&quot;'&gt;]+/i', $data, $media);
unset($data);
$data=preg_replace('/(img|src)(&quot;|'|=&quot;|=')(.*)/i',&quot;$3&quot;,$media[0]);
foreach($data as $url)
{
$info = pathinfo($url);
if (isset($info['extension']))
{
if (($info['extension'] == 'jpg') ||
($info['extension'] == 'jpeg') ||
($info['extension'] == 'gif') ||
($info['extension'] == 'png'))
array_push($images, $url);
}
}
</pre>
<p><strong>Source:</strong> <a href="http://www.catswhocode.com/blog/15-php-regular-expressions-for-web-developers">15 PHP regular expressions for web developers</a></p>
<p>Feed-parsing is an excellent use for this, as you have just the article, no layout-related imagery, like you would see if you were screen-scraping a web page to obtain the image URLs. Though I imagine <a href="http://digg.com">Digg</a> takes the latter route when they dig-up (Freudian pun unintended, honest) the thumbnails that go along with their news links.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.webmaster-source.com/2009/07/30/regular-expression-to-get-all-image-urls-from-a-page/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Parse a WordPress Plugin&#8217;s README.txt With Regular Expressions</title>
		<link>https://www.webmaster-source.com/2009/07/08/parse-a-wordpress-plugins-readme-txt-with-regular-expressions/</link>
		<comments>https://www.webmaster-source.com/2009/07/08/parse-a-wordpress-plugins-readme-txt-with-regular-expressions/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 11:08:40 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=2340</guid>
		<description><![CDATA[I&#8217;ve been working on a neat enhancement for my Tweetable WordPress plugin. Already I have a handy &#8220;Documentation&#8221; link on the plugin&#8217;s pages in the WordPress admin. When clicked, it opens a ThickBox dialog pointing to the README.txt file. Not bad, but it had a few rough edges. Raw markdown doesn&#8217;t look look stellar, and [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been working on a neat enhancement for my <a href="http://www.webmaster-source.com/tweetable-twitter-plugin-wordpress/">Tweetable</a> WordPress plugin. Already I have a handy &#8220;Documentation&#8221; link on the plugin&#8217;s pages in the WordPress admin. When clicked, it opens a <a href="http://jquery.com/demo/thickbox/">ThickBox</a> dialog pointing to the README.txt file.</p>
<p>Not bad, but it had a few rough edges. Raw <a href="http://daringfireball.net/projects/markdown/">markdown</a> doesn&#8217;t look look stellar, and then there was the problem with the horizontal scrollbars that would appear from loading a plain text file into the ThickBox. So I made a new script that would load-up the README.txt file and use Regular Expressions to parse some of the more basic markdown syntax into good old HTML.</p>
<p style="text-align: center;"><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-2341 imgborder" title="README.txt, parsed into HTML" src="//www.webmaster-source.com/wp-content/uploads/readme-parser.jpg" alt="README.txt, parsed into HTML" width="543" height="355" /></p>
<p><span id="more-2340"></span>As I write this, the changes haven&#8217;t been released to the public quite yet, as I have a few more things to finish up before putting out a new patch to the plugin, but they&#8217;re on their way.</p>
<p>How do you pull off something like this? It&#8217;s not too hard.</p>
<p>First, dump a basic HTML page wrapper into your new PHP file:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;
&lt;title&gt;Documentation&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Now, between the two body tags, we&#8217;ll put the beginnings of our script. We need to reference wp-load.php, so we can access a few WordPress-related functions later.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
require_once('../../../wp-load.php');
</pre>
<p>Now it&#8217;s time to load the README.txt file. Once we dump the contents into a variable, we run them through a series of functions. <code>wp_specialchars()</code> to escape PHP code and other unpleasant things, <code>nl2br()</code> to turn each newline character into a <code>&lt;br /&gt;</code> tag (which makes the text nice and readable, instead of a jumbled mess), and finally <code>make_clickable()</code> to turn any URLs into clickable links.</p>
<pre class="brush: php; title: ; notranslate">
$readme = file_get_contents('readme.txt');
$readme = make_clickable(nl2br(wp_specialchars($readme)));
</pre>
<p>With that out of the way, we move on to actually parsing some of the markdown formatting. Let&#8217;s start with turning backticks (`) into HTML &lt;code&gt; and &lt;/code&gt; tags.</p>
<pre class="brush: php; title: ; notranslate">
$readme = preg_replace('/`(.*?)`/', '&lt;code&gt;\\1&lt;/code&gt;', $readme);
</pre>
<p>It may look a bit&#8230;strange, but that line does just as advertised. The <em>/</em> characters signify the start and end of a Regular Expression, and the middle part isn&#8217;t too hard to guess at. The backticks are the markdown formatting we see wrapping a section of code (e.g. <code>`echo $this;`</code>) The part between the backticks, enclosed by the parenthesis, means &#8220;one or more of any sort of letter, number, or character.&#8221; The second argument of <code>preg_replace()</code> is the part we&#8217;ll be replacing the matches with, code tags with the content inside the backticks (represented as <code>\\1</code>) inside them.</p>
<p>Now we do a similar thing for <em>*italics*</em> and <strong>**bold text**</strong>. It&#8217;s important to put the line for the boldface formatting <em>before</em> the one for the italics, otherwise you&#8217;ll have some Unexpected Results happening.</p>
<pre class="brush: php; title: ; notranslate">
$readme = preg_replace('/[\040]\*\*(.*?)\*\*/', ' &lt;strong&gt;\\1&lt;/strong&gt;', $readme);
$readme = preg_replace('/[\040]\*(.*?)\*/', ' &lt;em&gt;\\1&lt;/em&gt;', $readme);
</pre>
<p>This one looks like more of a mess, doesn&#8217;t it? That&#8217;s because we have to escape the asterisks with backslashes (i.e. <code>\*</code>), as the asterisk has meaning in a regular expression otherwise. The <code>[\040]</code>, which represents a space character, is added so the expression will only match instances where the first asterisk has a space in front of it. This is mainly a safety feature, so no code snippets break anything&#8230;</p>
<p>Next we handle headings, which are marked-up as one to three equality signs on either side of a line of text.</p>
<pre class="brush: php; title: ; notranslate">
$readme = preg_replace('/=== (.*?) ===/', '&lt;h2&gt;\\1&lt;/h2&gt;', $readme);
$readme = preg_replace('/== (.*?) ==/', '&lt;h3&gt;\\1&lt;/h3&gt;', $readme);
$readme = preg_replace('/= (.*?) =/', '&lt;h4&gt;\\1&lt;/h4&gt;', $readme);
</pre>
<p>Once again, the order of the lines matters.</p>
<p>Now all that needs to be done is to echo-out the text and close our PHP block:</p>
<pre class="brush: php; title: ; notranslate">
echo $readme;
?&gt;
</pre>
<p>That wasn&#8217;t too hard was it? It&#8217;s only the most basic markdown syntax that&#8217;s being parsed, but it&#8217;s lightyears better than plain text.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.webmaster-source.com/2009/07/08/parse-a-wordpress-plugins-readme-txt-with-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/


Served from: www.webmaster-source.com @ 2026-06-10 04:25:58 by W3 Total Cache
-->