<?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>Tutorials Archive &#187; PHP</title>
	<atom:link href="http://tutarchive.com/category/web-design-development/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://tutarchive.com</link>
	<description>Save and Share your Tutorials</description>
	<lastBuildDate>Thu, 04 Feb 2010 02:51:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>vBulletin Variables List</title>
		<link>http://tutarchive.com/2010/02/vbulletin-variables-list/</link>
		<comments>http://tutarchive.com/2010/02/vbulletin-variables-list/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 02:51:45 +0000</pubDate>
		<dc:creator>TutArchive</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Design & Development]]></category>
		<category><![CDATA[vBulletin]]></category>

		<guid isPermaLink="false">http://tutarchive.com/?p=256</guid>
		<description><![CDATA[Site Statistics:-

$totalthreads - Displays the total number of threads in the forum
 $totalposts - Displays total number of posts in the forum
 $numbermembers - Display total number of members in the forum
 $totalonline - Displays the number of total users currently online
 $numberregistered - Displays the number of total registered users currently online
 $numberguest - [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p><strong>Site Statistics:-</strong></p>
<ul>
<li>$totalthreads - Displays the total number of threads in the forum</li>
<li> $totalposts - Displays total number of posts in the forum</li>
<li> $numbermembers - Display total number of members in the forum</li>
<li> $totalonline - Displays the number of total users currently online</li>
<li> $numberregistered - Displays the number of total registered users currently online</li>
<li> $numberguest - Displays the number of total guests currently online</li>
<li> $recordusers - Displays the number of most users ever online</li>
<li> $recorddate - Displays the date on which most users were ever online</li>
<li> $recordtime - Displays the time on which most users were ever online</li>
<li> $activeusers - Displays a list of currently active/online Users (i.e. logged in)</li>
<li> $activemembers - Displays the number of members who are active in the forum</li>
<li> $newusername - Displays the username of the newly registered user</li>
<li> $birthdays - Displays list of users whose birthday is today</li>
<li> $upcomingevents - Displays list of upcoming events</li>
</ul>
<p><span id="more-256"></span><br />
<strong>$BBuserinfo Array, this can show anything thats stored in the user table</strong></p>
<ul>
<li>$bbuserinfo[username] - Username</li>
<li>$bbuserinfo[userid] - User ID number</li>
<li>$bbuserinfo[posts] - Users postcount</li>
<li>$bbuserinfo[signature] - Users signature</li>
<li>$bbuserinfo[email] - User Email Address</li>
<li>$bbuserinfo[homepage] - Users homepage in profile</li>
<li>$bbuserinfo[msn] - Users MSN address from profile</li>
<li>$bbuserinfo[yahoo] - "" Yahoo ""</li>
<li>$bbuserinfo[icq] - "" ICQ ""</li>
<li>$bbuserinfo[usertitle] - Users custom usertitle</li>
</ul>
<p><strong>$VBoptions Array, these are the options stored in the settings table, i.e things that you configured from vbulletin AdminCP&gt;&gt;vbulletin Options.</strong></p>
<ul>
<li> $vboptions[forumactive] - Status of whether forum is turned on/off</li>
<li> $vboptions[bbclosedreason] - Reason why forum turned off</li>
<li> $vboptions[bbtitle] - Forum title</li>
<li> $vboptions[bburl] - Forum URL</li>
<li> $vboptions[hometitle] - Site homepage Title</li>
<li> $vboptions[homeurl] - Site homepage URL</li>
<li> $vboptions[copyrighttext] - Site copyright text</li>
<li> $vboptions[privacyurl] - URL to forum privacy statement</li>
<li> $vboptions[webmasteremail] - Webmaster email address</li>
<li> $vboptions[contactuslink] - contact us URL</li>
<li> $vboptiions[enablehooks] - Status of plugin/hook system</li>
<li> $vboptions[imagetype] - Determine if using GD or Imagemagick</li>
<li> $vboptions[timeoffset] - forum time offset</li>
<li> $vboptions[cookiepath] - forum cookie path</li>
<li> $vboptions[legacypostbit] - Determine if using legacy (side) postbit or new horizontal postbit</li>
<li> $vboptions[use_smtp] - If SMTP mail is enabled</li>
<li> $vboptions[smtp_host] - SMTP hostname</li>
<li> $vboptions[smtp_port] - SMTP port number</li>
<li> $vboptions[allowregistrations] - Determine if new users can register</li>
<li> $vboptions[welcomemail] - Determine if welcome mail is sent on registration</li>
<li> $vboptions[avatarenabled] - Check if avatars are enabled</li>
<li> $vboptions[reputationenable] - Check if reputation system is enabled</li>
</ul>
<p><strong>$stylevar Array, this contains all the paths to forum images and other style variables that are editable in the Styles and templates &gt;&gt; Stylevars</strong></p>
<ul>
<li> $stylevar[titleimage] - Path/filename for top banner</li>
<li> $stylevar[outertablewidth] - The width of your forum</li>
<li> $stylevar[spacersize] - Spacing betwen main body and content</li>
<li> $stylevar[cellspacing] - Inner border width</li>
<li> $stylevar[cellpadding] Table cell padding</li>
<li> $stylevar[formwidth] - Form width in pixels</li>
<li> $stylevar[imgdir_buton] - Path for forum buttons</li>
<li> $stylevar[imgdir_statusicon] - Path for status icons</li>
<li> $stylevar[imgdir_attach] - Path for attachment icons</li>
</ul>
<p><strong>$css array, this is where all the CSS attributed can be located</strong></p>
<ul>
<li> $css variables[body][background] - Body background colour</li>
<li> $css[body][color] - Body text colour</li>
<li> $css[body][LINK_N][color] - Body normal links</li>
<li> $css[body][LINK_V][color] - Body visited links</li>
<li> $css[body][LINK_M][color] - Body link hover colour</li>
<li> $css[.page][background] - Page background colour</li>
<li> $css[.page][color] - Page text colour</li>
<li> $css[.tborder][background] - Table border background colour</li>
<li> $css[.tborder][color] - Table border font colour</li>
<li> $css[.tcat][background] - Category strip background/colour</li>
<li> $css[.tcat][color] - Category strip font colour</li>
<li> $css[.thead][background] - Table header background gradient/colour</li>
<li> $css[.thead][color] - Table header background colour</li>
<li> $css[.tfoot][background] - Table footer background colour/gradient</li>
<li> $css[.tfoot][color] - Table footer text colour</li>
</ul>
<p><strong>Ad Location Template variables:-</strong></p>
<ul>
<li>$ad_location[ad_footer_end] - Displays content of ad_footer_end template</li>
<li> $ad_location[ad_footer_start] - Displays content of ad_footer_start template</li>
<li> $ad_location[ad_forumdisplay_afterthreads] - Displays content of ad_forumdisplay_afterthreads template</li>
<li> $ad_location[ad_forumdisplay_afterforums] - Displays content of ad_forumdisplay_afterforums template</li>
<li> $ad_location[ad_header_logo] - Displays content of ad_header_logo template</li>
<li> $ad_location[ad_header_end] - Displays content of ad_header_end template</li>
<li> $ad_location[ad_navbar_below] - Displays content of ad_navbar_below template</li>
<li> $ad_location[ad_showthread_beforeqr] - Displays content of ad_showthread_beforeqr template</li>
<li> $ad_location[ad_showthread_firstpost] - Displays content of ad_showthread_firstpost template</li>
</ul>
<p><strong><br />
Miscellenous:-</strong></p>
<ul>
<li>$notices - Used to display notices (for 3.7 onwards)</li>
<li> $notifications_total - Used to display total number of unread notifications (for 3.7 onwards)</li>
</ul>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://tutarchive.com/2010/02/vbulletin-variables-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>15 PHP regular expressions for web developers</title>
		<link>http://tutarchive.com/2009/09/15-php-regular-expressions-for-web-developers/</link>
		<comments>http://tutarchive.com/2009/09/15-php-regular-expressions-for-web-developers/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 15:07:26 +0000</pubDate>
		<dc:creator>TutArchive</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://tutarchive.com/?p=70</guid>
		<description><![CDATA[Regular expressions are a very useful tool for developers. They allow to find, identify or replace text, words or any kind of characters. In this article, I have compiled 15+ extremely useful regular expressions that any web developer should have in his toolkit.
Getting started with regular expressions
For many beginners, regular expressions seems to be hard [...]


Related posts:<ol><li><a href='http://tutarchive.com/2009/09/using-regex-regular-expressions-in-javascript/' rel='bookmark' title='Permanent Link: Using RegEx (Regular Expressions) in JavaScript'>Using RegEx (Regular Expressions) in JavaScript</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Regular expressions are a very useful tool for developers. They allow to find, identify or replace text, words or any kind of characters. In this article, I have compiled 15+ extremely useful regular expressions that any web developer should have in his toolkit.</p>
<h2>Getting started with regular expressions</h2>
<p>For many beginners, regular expressions seems to be hard to learn and use. In fact, they're far less hard than you may think. Before we dive deep inside regexp with useful and reusable codes, let's quickly see the basics:</p>
<h3>Regular expressions syntax</h3>
<table border="1" cellspacing="2" cellpadding="5" width="100%">
<tbody>
<tr>
<td>Regular Expression</td>
<td>Will match...</td>
</tr>
<tr>
<td>foo</td>
<td>The string "foo"</td>
</tr>
<tr>
<td>^foo</td>
<td>"foo" at the start of a string</td>
</tr>
<tr>
<td>foo$</td>
<td>"foo" at the end of a string</td>
</tr>
<tr>
<td>^foo$</td>
<td>"foo" when it is alone on a string</td>
</tr>
<tr>
<td>[abc]</td>
<td>a, b, or c</td>
</tr>
<tr>
<td>[a-z]</td>
<td>Any lowercase letter</td>
</tr>
<tr>
<td>[^A-Z]</td>
<td>Any character that is not a uppercase letter</td>
</tr>
<tr>
<td>(gif|jpg)</td>
<td>Matches either "gif" or "jpeg"</td>
</tr>
<tr>
<td>[a-z]+</td>
<td>One or more lowercase letters</td>
</tr>
<tr>
<td>[0-9.-]</td>
<td>Аny number, dot, or minus sign</td>
</tr>
<tr>
<td>^[a-zA-Z0-9_]{1,}$</td>
<td>Any word of at least one letter, number or _</td>
</tr>
<tr>
<td>([wx])([yz])</td>
<td>wy, wz, xy, or xz</td>
</tr>
<tr>
<td>[^A-Za-z0-9]</td>
<td>Any symbol (not a number or a letter)</td>
</tr>
<tr>
<td>([A-Z]{3}|[0-9]{4})</td>
<td>Matches three letters or four numbers</td>
</tr>
</tbody>
</table>
<h3><span id="more-70"></span></h3>
<h3>PHP regular expression functions</h3>
<table border="1" cellspacing="2" cellpadding="5" width="100%">
<tbody>
<tr>
<th width="10%">Function</th>
<th width="90%">Description</th>
</tr>
<tr>
<td>preg_match()</td>
<td>The preg_match() function searches string for pattern, returning true if pattern exists, and false otherwise.</td>
</tr>
<tr>
<td>preg_match_all()</td>
<td>The preg_match_all() function matches all occurrences of pattern in string.</td>
</tr>
<tr>
<td>preg_replace()</td>
<td>The preg_replace() function operates just like ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters.</td>
</tr>
<tr>
<td>preg_split()</td>
<td>The preg_split() function operates exactly like split(), except that regular expressions are accepted as input parameters for pattern.</td>
</tr>
<tr>
<td>preg_grep()</td>
<td>The preg_grep() function searches all elements of input_array, returning all elements matching the regexp pattern.</td>
</tr>
<tr>
<td>preg_ quote()</td>
<td>Quote regular expression characters</td>
</tr>
</tbody>
</table>
<h2>Validate domain name</h2>
<p>Verify if a string is a valid domain name.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://tutarchive.com/&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?/i'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Your url is ok.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Wrong url.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Enlight a word from a text</h2>
<p>This very useful regular expression find a specific word in a text, and enlight it. Extremely useful for search results.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Sample sentence from KomunitasWeb, regex has become popular in web programming. Now we learn regex. According to wikipedia, Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/b(regex)b/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&amp;lt;span style=&quot;background:#5fc9f6&quot;&amp;gt;1&amp;lt;/span&amp;gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$text</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Enlight search results in your WordPress blog</h2>
<p>As I just said that the previous code snippet could be very handy on search results, here is a great way to implement it on a WordPress blog.<br />
Open your <em>search.php</em> file and find the <em>the_title()</em> function. Replace it with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$title</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, just before the modified line, add this code:</p>
<p>Save the <em>search.php</em> file and open <em>style.css</em>. Append the following line to it:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">strong<span style="color: #339933;">.</span>search<span style="color: #339933;">-</span>excerpt <span style="color: #009900;">&#123;</span> background<span style="color: #339933;">:</span> yellow<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Get all images from a HTML document</h2>
<p>If you ever widhed to be able to get all images form a webpage, this code is a must have for you. You should easily create an image downloader using the power of <a href="http://www.catswhocode.com/blog/10-awesome-things-to-do-with-curl">cURL</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$images</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/(img|src)=(&quot;|'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>^<span style="color: #0000ff;">&quot;'&amp;gt;]+/i', <span style="color: #006699; font-weight: bold;">$data</span>, <span style="color: #006699; font-weight: bold;">$media</span>);
unset(<span style="color: #006699; font-weight: bold;">$data</span>);
<span style="color: #006699; font-weight: bold;">$data</span>=preg_replace('/(img|src)(&quot;</span><span style="color: #339933;">|</span><span style="color: #0000ff;">'|=&quot;|='</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span>i<span style="color: #0000ff;">',&quot;$3&quot;,$media[0]);
foreach($data as $url)
{
	$info = pathinfo($url);
	if (isset($info['</span>extension<span style="color: #0000ff;">']))
	{
		if (($info['</span>extension<span style="color: #0000ff;">'] == '</span>jpg<span style="color: #0000ff;">') ||
		($info['</span>extension<span style="color: #0000ff;">'] == '</span>jpeg<span style="color: #0000ff;">') ||
		($info['</span>extension<span style="color: #0000ff;">'] == '</span>gif<span style="color: #0000ff;">') ||
		($info['</span>extension<span style="color: #0000ff;">'] == '</span>png<span style="color: #0000ff;">'))
		array_push($images, $url);
	}
}</span></pre></div></div>

<h2>Remove repeated words (case insensitive)</h2>
<p>Often repeating words while typing? This handy regexp will be very helpful.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/s(w+s)1/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Remove repeated punctuation</h2>
<p>Same as above, but with punctuation. Goodbye repeated commas!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/.+/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Matching a XML/HTML tag</h2>
<p>This simple function takes two arguments: The first is the tag you'd like to match, and the second is the variable containing the XML or HTML. Once again, this can be very powerful used along with <a href="http://www.catswhocode.com/blog/10-awesome-things-to-do-with-curl">cURL</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_tag<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$tag</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$tag</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'{&amp;lt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$tag</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'[^&amp;gt;]*&amp;gt;(.*?)&amp;lt;/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$tag</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;gt;.'</span><span style="color: #009900;">&#125;</span><span style="color: #0000ff;">',
                   $xml,
                   $matches,
                   PREG_PATTERN_ORDER);
&nbsp;
  return $matches[1];
}</span></pre></div></div>

<h2>Matching an XHTML/XML tag with a certain attribute value</h2>
<p>This function is very similar to the previous one, but it allow you to match a tag having a specific attribute. For example, you could easily match <em>&lt;div id="header"&gt;</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_tag<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$attr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tag</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">is_null</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$tag</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'\w+'</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">else</span>
    <span style="color: #000088;">$tag</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$attr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$tag_regex</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/&amp;lt;(&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$tag</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;)[^&amp;gt;]*<span style="color: #006699; font-weight: bold;">$attr</span>\s*=\s*&quot;</span><span style="color: #339933;">.</span>
                <span style="color: #0000ff;">&quot;(['<span style="color: #000099; font-weight: bold;">\&quot;</span>])<span style="color: #006699; font-weight: bold;">$value</span><span style="color: #000099; font-weight: bold;">\\</span>2[^&amp;gt;]*&amp;gt;(.*?)&amp;lt;\/<span style="color: #000099; font-weight: bold;">\\</span>1&amp;gt;/&quot;</span>
&nbsp;
  <span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tag_regex</span><span style="color: #339933;">,</span>
                 <span style="color: #000088;">$xml</span><span style="color: #339933;">,</span>
                 <span style="color: #000088;">$matches</span><span style="color: #339933;">,</span>
                 PREG_PATTERN_ORDER<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Matching hexadecimal color values</h2>
<p>Another interesting tool for web developers! It allows you to match/validate a hexadecimal color value.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;#555555&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^#(?:(?:[a-fd]{3}){1,2})$/i'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;example 6 successful.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Find page title</h2>
<p>This handy code snippet will find and print the text within the <em>&lt;title&gt;</em> and <em>&lt;/title&gt;</em> tags of a html page.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://tutarchive.com/blog&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$page</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">fgets</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4096</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$titre</span> <span style="color: #339933;">=</span> <span style="color: #990000;">eregi</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;title&amp;gt;(.*)&amp;lt;/title&amp;gt;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$page</span><span style="color: #339933;">,</span><span style="color: #000088;">$regs</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$regs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Parsing Apache logs</h2>
<p>Most websites are running on the well-known Apache webserver. If your website does, what about using PHP and some regular expressions to parse Apache logs?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//Logs: Apache web server</span>
<span style="color: #666666; font-style: italic;">//Successful hits to HTML files only.  Useful for counting the number of page views.</span>
<span style="color: #0000ff;">'^((?#client IP or domain name)S+)s+((?#basic authentication)S+s+S+)s+[((?#date and time)[^]]+)]s+&quot;(?:GET|POST|HEAD) ((?#file)/[^ ?&quot;]+?.html?)??((?#parameters)[^ ?&quot;]+)? HTTP/[0-9.]+&quot;s+(?#status code)200s+((?#bytes transferred)[-0-9]+)s+&quot;((?#referrer)[^&quot;]*)&quot;s+&quot;((?#user agent)[^&quot;]*)&quot;$'</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Logs: Apache web server</span>
<span style="color: #666666; font-style: italic;">//404 errors only</span>
<span style="color: #0000ff;">'^((?#client IP or domain name)S+)s+((?#basic authentication)S+s+S+)s+[((?#date and time)[^]]+)]s+&quot;(?:GET|POST|HEAD) ((?#file)[^ ?&quot;]+)??((?#parameters)[^ ?&quot;]+)? HTTP/[0-9.]+&quot;s+(?#status code)404s+((?#bytes transferred)[-0-9]+)s+&quot;((?#referrer)[^&quot;]*)&quot;s+&quot;((?#user agent)[^&quot;]*)&quot;$'</span></pre></div></div>

<h2>Replacing double quotes by smart qutotes</h2>
<p>If you're a typographer lover, you'll probably love this regexp, which allow you to replace normal double quotes by smart quotes. A regular expression of that kind is used by WordPress on contents.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'B&quot;b([^&quot;x84x93x94rn]+)b&quot;B'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'?1?'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Checking password complexity</h2>
<p>This regular expression will tests if the input consists of 6 or more letters, digits, underscores and hyphens.<br />
The input must contain at least one upper case letter, one lower case letter and one digit.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">'A(?=[-_a-zA-Z0-9]*?[A-Z])(?=[-_a-zA-Z0-9]*?[a-z])(?=[-_a-zA-Z0-9]*?[0-9])[-_a-zA-Z0-9]{6,}z'</span></pre></div></div>

<h2>WordPress: Using regexp to retrieve images from post</h2>
<p>As I know many of you are WordPress users, you'll probably enjoy that code which allow you to retrieve all images from post content and display it.<br />
To use this code on your blog, simply paste the following code on one of your theme files.</p>

<div class="wp_syntax"><div class="code"><pre class="code" style="font-family:monospace;">&nbsp;
post_content;
$szSearchPattern = '~&lt;img alt=&quot;&quot; /&gt;]* /&amp;gt;~';
&nbsp;
// Run preg_match_all to grab all the images and save the results in $aPics
preg_match_all( $szSearchPattern, $szPostContent, $aPics );
&nbsp;
// Check to see if we have at least 1 image
$iNumberOfPics = count($aPics[0]);
&nbsp;
if ( $iNumberOfPics &amp;gt; 0 ) {
     // Now here you would do whatever you need to do with the images
     // For this example the images are just displayed
     for ( $i=0; $i &amp;lt; $iNumberOfPics ; $i++ ) {           echo $aPics[0][$i];      }; }; endwhile; endif; ?&amp;gt;</pre></div></div>

<h2>Generating automatic smileys</h2>
<p>Another function used by WordPress, this one allow you to automatically replace a smiley symbol by an image.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$texte</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'A text with a smiley :-)'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':-)'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&amp;lt;img src=&quot;smileys/souriant.png&quot;&amp;gt;'</span><span style="color: #339933;">,</span><span style="color: #000088;">$texte</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>



<p>Related posts:<ol><li><a href='http://tutarchive.com/2009/09/using-regex-regular-expressions-in-javascript/' rel='bookmark' title='Permanent Link: Using RegEx (Regular Expressions) in JavaScript'>Using RegEx (Regular Expressions) in JavaScript</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://tutarchive.com/2009/09/15-php-regular-expressions-for-web-developers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
