<?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>preg_match &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/preg_match/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>PHP: preg_match Give Names to the Matches</title>
		<link>/2010/07/03/php-preg_match-give-names-to-the-matches/</link>
		<comments>/2010/07/03/php-preg_match-give-names-to-the-matches/#respond</comments>
		<pubDate>Sat, 03 Jul 2010 17:57:28 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Associative array]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[maintainable code]]></category>
		<category><![CDATA[matches]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[preg_match]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">/?p=1725</guid>
		<description><![CDATA[So Called &#8211; Subpatterns In PHP 5.2.2+ you can name the sub patterns returned from preg_match with a specific syntax. Named subpatterns now accept the syntax (?&#60;name&#62;) and (?&#8217;name&#8217;) as well as (?P&#60;name&#62;). Previous versions accepted only (?P&#60;name&#62;) This is extremely helpful, when dealing with long patterns. As you may know you can simply use &#8230; <a href="/2010/07/03/php-preg_match-give-names-to-the-matches/" class="more-link">Continue reading <span class="screen-reader-text">PHP: preg_match Give Names to the Matches</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/08/31/php-what-is-more-powerful-than-list-perhaps-extract/" rel="bookmark" title="PHP: What is More Powerful Than list() &#8211; Perhaps extract()">PHP: What is More Powerful Than list() &#8211; Perhaps extract() </a></li>
<li><a href="/2010/04/29/debugging-in-zend-framework/" rel="bookmark" title="Debugging in Zend Framework">Debugging in Zend Framework </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/" rel="bookmark" title="How to Collect the Images and Meta Tags from a Webpage with PHP">How to Collect the Images and Meta Tags from a Webpage with PHP </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>So Called &#8211; Subpatterns</h2>
<p><a href="/wp-content/uploads/2010/07/patterns.jpeg"><img src="/wp-content/uploads/2010/07/patterns.jpeg" alt="Patterns - preg_match" title="patterns" width="430" height="89" class="aligncenter size-full wp-image-1767" srcset="/wp-content/uploads/2010/07/patterns.jpeg 430w, /wp-content/uploads/2010/07/patterns-300x62.jpg 300w" sizes="(max-width: 430px) 100vw, 430px" /></a></p>
<p>In PHP 5.2.2+ you can name the sub patterns returned from <a href="http://php.net/manual/en/function.preg-match.php" target="_blank">preg_match</a> with a specific syntax.</p>
<blockquote><p>Named subpatterns now accept the syntax (?&lt;name&gt;)  and (?&#8217;name&#8217;) as well as (?P&lt;name&gt;). Previous versions accepted only (?P&lt;name&gt;)</p></blockquote>
<p>This is extremely helpful, when dealing with long patterns. As you may know you can simply use the &#8220;old school&#8221; way and to call the matches by their number based index:</p>
<pre lang="php">
    $haystack = '01 Jan 1970';
    $pattern = '/(\d{1,2})\ (Jan|Feb)\ (19\d\d)/';

    preg_match($pattern, $haystack, $matches);

    print_r($matches);
</pre>
<p>Although it may look difficult to maintain, now you can simply name the sub patterns of preg_match and to call them with their associative array keys. This is more clear when writing code and it&#8217;s definitely more maintainable.</p>
<pre lang="php">
    $haystack = '01 Jan 1970';
    $pattern = '/(?<day>\d{1,2})\ (?<month>Jan|Feb)\ (?<year>19\d\d)/';

    preg_match($pattern, $haystack, $matches);

    print_r($matches);

    // now there's $matches['day'], $matches['month'] ...
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/08/31/php-what-is-more-powerful-than-list-perhaps-extract/" rel="bookmark" title="PHP: What is More Powerful Than list() &#8211; Perhaps extract()">PHP: What is More Powerful Than list() &#8211; Perhaps extract() </a></li>
<li><a href="/2010/04/29/debugging-in-zend-framework/" rel="bookmark" title="Debugging in Zend Framework">Debugging in Zend Framework </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/" rel="bookmark" title="How to Collect the Images and Meta Tags from a Webpage with PHP">How to Collect the Images and Meta Tags from a Webpage with PHP </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/07/03/php-preg_match-give-names-to-the-matches/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
