<?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>A Tasty Pixel » Blog &#187; Regex</title>
	<atom:link href="http://atastypixel.com/blog/tag/regex/feed/" rel="self" type="application/rss+xml" />
	<link>http://atastypixel.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 06 Feb 2012 15:08:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Reginald RegEx explorer</title>
		<link>http://atastypixel.com/blog/reginald-regex-explorer/</link>
		<comments>http://atastypixel.com/blog/reginald-regex-explorer/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 22:28:27 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Regex]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/reginald-regex-explorer/</guid>
		<description><![CDATA[With a desperate need to debug a lengthy regular expression destined for use with the excellent RegexKitLite library, I have quickly put together a Mac OS X application. Reginald is a kindly old gentleman devoted to assisting you with those tricky regular expressions. Provide some sample input, and your regular expression, and Reginald will provide [...]]]></description>
			<content:encoded><![CDATA[<p>With a desperate need to debug a lengthy regular expression destined for use with the excellent <a href="http://regexkit.sourceforge.net/RegexKitLite/">RegexKitLite</a> library, I have quickly put together a Mac OS X application.</p>

<p><img src="http://atastypixel.com/blog/wp-content/uploads/2010/07/reg.png" width="64" height="69" alt="Reginald icon" style="float:right;" />
Reginald is a kindly old gentleman devoted to assisting you with those tricky regular expressions.</p>

<p>Provide some sample input, and your regular expression, and Reginald will provide you with colour-coded output and a list of all your matches and the corresponding capture groups for your exploration.  Select a match or capture group in the list to the right, and the corresponding text will be selected in the panel to the left.</p>

<p>Reginald is built on <a href="http://regexkit.sourceforge.net/RegexKitLite/">RegexKitLite</a>, and so uses the <a href="http://regexkit.sourceforge.net/RegexKitLite/index.html#ICUSyntax">ICU syntax</a>.</p>

<p>It will run on Mac OS X 10.6 and above.</p>

<p><a href="http://github.com/michaeltyson/Reginald/downloads">Download Reginald here</a>, or <a href="http://github.com/michaeltyson/Reginald">access the source on GitHub</a>.</p>

<p><a href="http://atastypixel.com/blog/wp-content/uploads/2010/07/201007312318.jpg" rel="lightbox[1966]"><img src="http://atastypixel.com/blog/wp-content/uploads/2010/07/201007312318-tm.jpg" width="450" height="342" alt="Reginald screenshot" class="aligncenter" /></a></p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1966" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/reginald-regex-explorer/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Advanced web templating with PHP and regular expressions</title>
		<link>http://atastypixel.com/blog/advanced-web-templating-with-php-and-regular-expressions/</link>
		<comments>http://atastypixel.com/blog/advanced-web-templating-with-php-and-regular-expressions/#comments</comments>
		<pubDate>Sun, 12 Oct 2008 02:53:03 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Regex]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/2008/10/12/advanced-web-templating-with-php-and-regular-expressions/</guid>
		<description><![CDATA[A method to gain more control over the styling of images for web templates/blog themes, using a bit of Regular Expression magic. This is the technique used for my recent theme, Elegant Grunge.]]></description>
			<content:encoded><![CDATA[<p><em>This post describes a method to gain more control over the styling of images for web templates/blog themes, using a bit of Regular Expression magic. This is the technique used for my recent theme, <a href="http://atastypixel.com/blog/2008/10/10/elegant-grunge-wordpress-theme/">Elegant Grunge</a>.</em></p>

<p>You&#8217;ve just designed the most beautiful template ever &#8211; pleasing composition, clean lines, smooth colours and gradients and an awesome typeface. You apply it as the theme for your blog, or release it to the community, and there is much rejoicing.<a href="http://atastypixel.com/blog/wp-content/uploads/2008/10/200810121259.jpg" rel="lightbox[921]"><img src="http://atastypixel.com/blog/wp-content/uploads/2008/10/200810121259-tm.jpg" width="100" height="67" alt="Example of the 'SH Trocadero' WordPress theme with image" title="Example of the 'SH Trocadero' WordPress theme with image" style="float:right;" /></a></p>

<p>Then you or someone else inconsiderately puts a photograph into a post &#8211; disaster! That nasty square of graphic just destroyed your composition, and the page is unbalanced and suddenly looks terrible.</p>

<p>Images are particularly tricky to make work with a theme, mostly because they&#8217;re so unpredictable. With text, it&#8217;s easy to control the way it looks. With images, they can be any colour or shape, many of which will break the composition of a theme, especially if the design elements of the theme contains irregular lines.</p>

<p>A big impediment to making images work is the limitations introduced by the current versions of CSS: All you can really do is provide a square border, possibly with some padding. That&#8217;s it, unless you&#8217;re willing to manually add tons of markup around images every time – and willing to make your users do it, if you&#8217;ve released a theme.<a href="http://atastypixel.com/blog/wp-content/uploads/2008/10/200810121308.jpg" rel="lightbox[921]"><img src="http://atastypixel.com/blog/wp-content/uploads/2008/10/200810121308-tm.jpg" width="100" height="71" alt="Example of images in the &quot;Green Light&quot; WordPress theme" title="Example of images in the &quot;Green Light&quot; WordPress theme" style="float:right;" /></a></p>

<p>Some people will carefully Photoshop-up their images to make them fit &#8211; <a href="http://webdemar.com/">Webdemar</a> (who made the theme I first used on my blog) does this, for example, and it looks great. That&#8217;s a lot of work, though, especially if you make frequent posts with images!</p>

<p>If you&#8217;re a theme designer, there&#8217;s an easier way to make images work.</p>

<p><span id="more-921"></span></p>

<h3>CSS Borders</h3>

<p>There are a <a href="http://www.google.com/search?q=css+rounded+borders+OR+corners">thousand and one</a> different ways to create styled borders in CSS. The basic principle is that one adds a certain amount of HTML markup around the content to be framed, and CSS rules apply background images in a variety of ways. The best of these techniques are very flexible, and allow designers to apply any kind of frame they can imagine.</p>

<p>The Joomla documentation describes <a href="http://docs.joomla.org/Creating_rounded_corners">one of these techniques</a> quite well, and it’s worth a read.</p>

<p>So, providing a frame around images allows designers to break up those straight lines, among other things. Thus, this:</p>

<div style="text-align: center;">
  <a href="http://atastypixel.com/blog/wp-content/uploads/2008/10/200810121311.jpg" rel="lightbox[921]"><img src="http://atastypixel.com/blog/wp-content/uploads/2008/10/200810121311-tm.jpg" width="300" height="216" alt="Example of &quot;Elegant Grunge&quot; WordPress theme without framed images" title="Example of &quot;Elegant Grunge&quot; WordPress theme without framed images" /></a><br />
</div>

<div style="text-align: center;">
  <small><em>Note how the straight image border breaks the ‘broken-up’ edge theme</em></small><br />
</div>

<p>Becomes this:</p>

<div style="text-align: center;">
  <a href="http://atastypixel.com/blog/wp-content/uploads/2008/10/200810121312.jpg" rel="lightbox[921]"><img src="http://atastypixel.com/blog/wp-content/uploads/2008/10/200810121312-tm.jpg" width="300" height="205" alt="200810121312.jpg" title="200810121312.jpg" class="Example of "Elegant Grunge" WordPress theme with framed images" /></a><br />
</div>

<div style="text-align: center;">
  <small><em>Frame with drop shadow provides illusion of depth, distancing image from theme</em></small><br />
</div>

<p>This is, of course, just one example, but there are many possibilities.</p>

<p>So, the question becomes: How do we provide the necessary HTML markup in the content, without doing it ourselves or making our users do it? Enter regular expressions…</p>

<h3>Regex magic</h3>

<p>The concept is very simple – whenever we encounter an image in the content, we surround it by the markup necessary to render a frame around it. There are, however, a few extra things that’d be nice:</p>

<ul>
  <li>Provide the ability to frame any content, not just images, by supplying a class</li>

  <li>Provide the ability to avoid framing some images, to provide some control, by supplying a class</li>

  <li>Manage image sizing, in case a too-large image is provided</li>

  <li>Avoid showing frames if the surrounded image is too small (and would thus look wrong)</li>

  <li>Some styles/classes applied to the inner content should be also applied to the frame (such as float settings)</li>
</ul>

<p>This PHP script gives an example of how it can be done as part of a WordPress theme: <a href="http://atastypixel.com/blog/wp-content/uploads/2008/10/framing-samplephp.zip">framing-sample.php.zip</a></p>

<p>The script provides a filter function, used by WordPress during the render pipeline to modify content. The function looks for blocks with a class of ‘frame’, or images. It avoids any block with a class of ‘noframe’ (and also ‘wp-smiley’, which is the class used for those small emoticons in WordPress). It then uses a ‘callback’ helper function to perform the actual replacement, capturing styles/classes for the inner object, and managing image sizes in the process.</p>

<p>A caveat here: The regular expression that supports wrapping any kind of content block (as opposed to just images) uses a relatively modern feature of regular expressions called recursive sub-patterns, which will not work with PHP installations with an older PCRE regular expression library. Hence, the ‘@’ sign preceding the preg_replace_callback, which will cause it to fail silently, without throwing error messages everywhere.</p>

<p>So, this code wraps the content in four ‘span’ objects, which can then be styled using CSS. For small images, it also applies the ‘small’ class, which can be used to provide a smaller frame to avoid visual glitching.</p>

<h3>CSS time</h3>

<p>So, we now have markup in place, and it’s time to provide some styles. Here’s some sample CSS:</p>

<blockquote>
  <br /><span style="color: #8C868F; font-size: 11px;"><span style="color: #8c868f">/* Frame */</span><span style="color: #000000"><br />  <br /></span>.frame-outer <span style="color: #000000">{<br /></span> background<span style="color: #000000">:</span> url<span style="color: #000000">(images/frame-top-left.jpg)</span> no-repeat left top<span style="color: #000000">;<br /></span> padding<span style="color: #000000">:</span> 0<span style="color: #000000">;<br /></span> margin<span style="color: #000000">:</span> 0<span style="color: #000000">;<br /></span> display<span style="color: #000000">:</span> inline-block<span style="color: #000000">;<br />}<br />  <br /></span>.frame-outer span <span style="color: #000000">{<br /></span> background<span style="color: #000000">:</span> url<span style="color: #000000">(images/frame-bottom-left.jpg)</span> no-repeat left bottom<span style="color: #000000">;<br /></span> margin<span style="color: #000000">:</span> 0<span style="color: #000000">;<br /></span> padding<span style="color: #000000">:</span> 0<span style="color: #000000">;<br /></span> display<span style="color: #000000">:</span> block<span style="color: #000000">;<br />}<br />  <br /></span>.frame-outer span span <span style="color: #000000">{<br /></span> background<span style="color: #000000">:</span> url<span style="color: #000000">(images/frame-top-right.jpg)</span> no-repeat right top<span style="color: #000000">;<br /></span> display<span style="color: #000000">:</span> block<span style="color: #000000">;<br />}<br />  <br /></span>.frame-outer span span span <span style="color: #000000">{<br /></span> background<span style="color: #000000">:</span> url<span style="color: #000000">(images/frame-bottom-right.jpg)</span> no-repeat right bottom<span style="color: #000000">;<br /></span> padding<span style="color: #000000">:</span> 32<span style="color: #ff7800">px</span><span style="color: #000000">;<br /></span> min-width<span style="color: #000000">:</span> 150<span style="color: #ff7800">px</span><span style="color: #000000">;<br /></span> min-height<span style="color: #000000">:</span> 150<span style="color: #ff7800">px</span><span style="color: #000000">;<br /></span> text-align<span style="color: #000000">:</span> center<span style="color: #000000">;<br /></span> overflow<span style="color: #000000">:</span> hidden<span style="color: #000000">;<br /></span> display<span style="color: #000000">:</span> block<span style="color: #000000">;<br />}<br />  <br /></span>.frame-outer span span span * <span style="color: #000000">{<br /></span> max-width<span style="color: #000000">:</span> 425<span style="color: #ff7800">px</span><span style="color: #000000">;<br />}<br />  <br /></span>.frame-outer.small <span style="color: #000000">{<br /></span> background<span style="color: #000000">:</span> url<span style="color: #000000">(images/frame-top-left-small.jpg)</span> no-repeat left top<span style="color: #000000">;<br />}<br />  <br /></span>.frame-outer.small span <span style="color: #000000">{<br /></span> background<span style="color: #000000">:</span> url<span style="color: #000000">(images/frame-bottom-left-small.jpg)</span> no-repeat left bottom<span style="color: #000000">;<br />}<br />  <br /></span>.frame-outer.small span span <span style="color: #000000">{<br /></span> background<span style="color: #000000">:</span> url<span style="color: #000000">(images/frame-top-right-small.jpg)</span> no-repeat right top<span style="color: #000000">;<br />}<br />  <br /></span>.frame-outer.small span span span <span style="color: #000000">{<br /></span> background<span style="color: #000000">:</span> url<span style="color: #000000">(images/frame-bottom-right-small.jpg)</span> no-repeat right bottom<span style="color: #000000">;<br /></span> padding<span style="color: #000000">:</span> 10<span style="color: #ff7800">px</span><span style="color: #000000">;<br /></span> min-height<span style="color: #000000">:</span> 38<span style="color: #ff7800">px</span><span style="color: #000000">;<br /></span> min-width<span style="color: #000000">:</span> 38<span style="color: #ff7800">px</span><span style="color: #000000">;<br />}<br />  <br /></span>.frame-outer span span span span <span style="color: #000000">{<br /></span> background<span style="color: #000000">:</span> none<span style="color: #000000">;<br />}</span></span>
</blockquote>

<p>Here’s a sample of the images that are used here:</p>

<div style="text-align: center;">
  <img src="http://atastypixel.com/blog/wp-content/uploads/2008/10/framing-images.jpg" width="400" height="400" alt="framing-images.jpg" title="framing-images.jpg" class="noframe" /><br />
</div>

<h3>Further possibilities</h3>

<p>Providing frames around image is all very well, but what if that’s not enough? Perhaps you have a particular style set up that requires, say, rounded corners, or Aqua-style bubbles, or sepia tones? Maybe something more complicated, as with Webdemar’s page: Rounded corners, a dark border with a light inner-border, a gradient background, then the image itself rotated a bit then cropped.</p>

<p>It just so happens, there’s a whole lot one can do with the PHP ‘GD’ image processing library, if you’re willing to get your hands dirty. Imagemagick’s ‘convert’ utility is fairly capable too. This is outside the scope of this article, but I may return to the topic another time.</p>

<p>So, one possibility is to add an additional function that is called, which extracts the URL of any image, opens and processes the image, saves it to a local cache, and replaces the URL with a URL to the newly created, cached image. This function could do all kinds of nifty operations on the image, with zero work required on the part of the user.</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=921" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/advanced-web-templating-with-php-and-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

