<?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>code-diesel &#187; pear</title>
	<atom:link href="http://www.codediesel.com/category/pear/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Disabling the silence @-operator in PHP</title>
		<link>http://www.codediesel.com/php/disabling-the-silence-operator-in-php/</link>
		<comments>http://www.codediesel.com/php/disabling-the-silence-operator-in-php/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 08:47:34 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[peck]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2673</guid>
		<description><![CDATA[How to disable the silence error operator in php]]></description>
			<content:encoded><![CDATA[<p>PHP supports one error control operator: the at sign (@). When prepended  to an expression any error generated by that expression will be ignored. It can also be useful for hiding errors generated by various functions.Take the following simple example:</p>

<div class="wp_codebox"><table><tr id="p26731"><td class="code" id="p2673code1"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'data'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If the &#8216;data&#8217; parameter is not defined the expression will generate an error.</p>

<div class="wp_codebox"><table><tr id="p26732"><td class="code" id="p2673code2"><pre class="php" style="font-family:monospace;">Notice<span style="color: #339933;">:</span> Undefined index<span style="color: #339933;">:</span> data in <span style="color: #339933;">/</span><span style="color: #000000; font-weight: bold;">var</span><span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>test<span style="color: #339933;">.</span>php on line <span style="color: #cc66cc;">9</span></pre></td></tr></table></div>

<p>You can hide the error using the silence @-operator.</p>

<div class="wp_codebox"><table><tr id="p26733"><td class="code" id="p2673code3"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span>  <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'data'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Although quite useful at some times, using the @-operator can have some annoying side effects. Say you are using some external libraries in your application which uses the @-operator. If everything works fine than good. But if the library is generating some errors than it becomes difficult to point the exact location where the error occurs, as the @-operator hides it. If the external library is large, it becomes a headache to remove all the @ from the code. One nice option I found is the <a href="http://pecl.php.net/package/scream" rel="nofollow" target="_blank" >Scream</a> Pecl extension. The extension allows you to easily disable the @-operator in your code without making any actual changes to the code.<br />
<span id="more-2673"></span></p>
<h4>Installing the Scream extension</h4>
<p>As a pre-complied binary is not available, you need to make it yourself. The following shows commands to compile the extension on Ubuntu.</p>
<p>First you need to install the Pear distribution environment.</p>

<div class="wp_codebox"><table><tr id="p26734"><td class="code" id="p2673code4"><pre class="shell" style="font-family:monospace;">sudo apt-get install php-pear</pre></td></tr></table></div>

<p>Next you will need to install the php5-dev package to get the required PHP5 source files to compile additional modules.</p>

<div class="wp_codebox"><table><tr id="p26735"><td class="code" id="p2673code5"><pre class="shell" style="font-family:monospace;">sudo apt-get install php5-dev</pre></td></tr></table></div>

<p>Finally we are ready to actually create and install the extension.</p>

<div class="wp_codebox"><table><tr id="p26736"><td class="code" id="p2673code6"><pre class="shell" style="font-family:monospace;">sudo pecl install scream-0.1.0</pre></td></tr></table></div>

<p>Once the extension is created and installed, we need to add one to the php.ini file.</p>

<div class="wp_codebox"><table><tr id="p26737"><td class="code" id="p2673code7"><pre class="shell" style="font-family:monospace;">sudo gedit /etc/php5/apache2/php.ini</pre></td></tr></table></div>

<p>In the &#8216;extensions&#8217; section add the following line:<br />
extension=scream.so;</p>
<p>After the php.ini has been updated, you need to restart Apache, so that the new extension is loaded.</p>

<div class="wp_codebox"><table><tr id="p26738"><td class="code" id="p2673code8"><pre class="shell" style="font-family:monospace;">sudo /etc/init.d/apache2 restart</pre></td></tr></table></div>

<p>If hopefully all went well, the Scream extension should now be loaded, which you can confirm using phpinfo().</p>
<h4>Breaking the Silence operator</h4>
<p>Now you can disable the @-operator in your code using the following:</p>

<div class="wp_codebox"><table><tr id="p26739"><td class="code" id="p2673code9"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'display_errors'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">error_reporting</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ALL</span> <span style="color: #339933;">|</span> <span style="color: #009900; font-weight: bold;">E_STRICT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Disable the @-operator</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'scream.enabled'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'data'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Or you can directly enable the extension in your php.ini.</p>

<div class="wp_codebox"><table><tr id="p267310"><td class="code" id="p2673code10"><pre class="php" style="font-family:monospace;">scream<span style="color: #339933;">.</span>enabled<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span></pre></td></tr></table></div>

<p>Now even though the silence operator is present the above code generates an error if the &#8216;data&#8217; parameter is not set. Atlast no need to hunt down for @&#8217;s while debugging.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/disabling-the-silence-operator-in-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Reading MP3 file tags in PHP</title>
		<link>http://www.codediesel.com/pear/reading-mp3-file-tags-in-php/</link>
		<comments>http://www.codediesel.com/pear/reading-mp3-file-tags-in-php/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 06:59:04 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[pear]]></category>
		<category><![CDATA[mp3]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2080</guid>
		<description><![CDATA[how to read MP3 ID3 file tags in PHP]]></description>
			<content:encoded><![CDATA[<p>I recently had a small task to scan some MP3 files on a server to check whether the files had any metadata in them, and add them if necessary. All MP3&#8242;s have a metadata section that allows information such as the title, artist, album, track number etc. to be stored in the MP3 file itself. The metadata is stored in the ID3 format. Of course I could have used a desktop tag editor, but the files where on the server, which frequently kept changing. So a server solution was the only way to go.<br />
<span id="more-2080"></span><br />
For the solution I used the Pear MP3_Id package. There are many PHP libraries around that support manipulation of MP3 metadata, but this one was the one I narrowed down. The following examples show how you can use the package to manipulate mp3&#8242;s metadata.</p>
<h4>Installing MP3_Id</h4>
<p>MP3_Id being a Pear package we will use the Pear installer. Or you can manually download the package from <a href="http://pear.php.net/package/MP3_Id/download" rel="nofollow" >Pear MP3_Id</a>.</p>

<div class="wp_codebox"><table><tr id="p208011"><td class="code" id="p2080code11"><pre class="text" style="font-family:monospace;">pear install MP3_Id-1.2.1</pre></td></tr></table></div>

<h4>Reading mp3 tags</h4>
<p>The following function reads the ID3v1 data from all the mp3 files in the given directory, returning the data as an array.</p>

<div class="wp_codebox"><table><tr id="p208012"><td class="code" id="p2080code12"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MP3/Id.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> read_mp3_tags<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    static <span style="color: #000088;">$result</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>
    static <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$tag_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$mp3</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">&amp;new</span> MP3_Id<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">// Tags supported by the MP3_Id class</span>
    <span style="color: #000088;">$tags</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                  <span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;artists&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;album&quot;</span><span style="color: #339933;">,</span>
                  <span style="color: #0000ff;">&quot;year&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;comment&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;track&quot;</span><span style="color: #339933;">,</span>
                  <span style="color: #0000ff;">&quot;genre&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;genreno&quot;</span>
                  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    <span style="color: #009933; font-style: italic;">// Read the current directory</span>
    <span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #990000;">dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">// Loop through all the files in the current directory:</span>
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">!==</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$d</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #009933; font-style: italic;">// Skip '.' and '..'</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'.'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'..'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">// If this is a directory, then recursively call it</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$dir}</span>/<span style="color: #006699; font-weight: bold;">{$file}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            read_mp3_tags<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$dir}</span>/<span style="color: #006699; font-weight: bold;">{$file}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #009933; font-style: italic;">// It's a mp3 file so read the tags</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;mp3&quot;</span><span style="color: #009900;">&#41;</span> 
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mp3</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$dir}</span>/<span style="color: #006699; font-weight: bold;">{$file}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #009933; font-style: italic;">// OOPs, some error occured, just save the filename</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>PEAR<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span> 
                    <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$file</span><span style="color: #339933;">;</span>
                    <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'directory'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dir</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000000; font-weight: bold;">else</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$file</span><span style="color: #339933;">;</span>
                    <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'directory'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dir</span><span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #009933; font-style: italic;">// Read all the tags of the particular file</span>
                    <span style="color: #000000; font-weight: bold;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tags</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mp3</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTag</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: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">// Call the above function on your mp3 directory</span>
<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> read_mp3_tags<span style="color: #009900;">&#40;</span>PATH_TO_YOUR_MP3_DIRECTORY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Writing or updating tags in an MP3</h4>
<p>After you have read ID3 tags, you may want to change or add some new tag information to the files. The following code for example adds the missing album name to the ID3 metadata.</p>
<p><b>Before you modify any files, be sure you backup them until you are sure your code works fine.</b></p>

<div class="wp_codebox"><table><tr id="p208013"><td class="code" id="p2080code13"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MP3/Id.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$mp3</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">&amp;new</span> MP3_Id<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Read the MP3 file</span>
<span style="color: #000088;">$mp3</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tears For Fears - Shout.mp3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Add the album name to the 'album' tag</span>
<span style="color: #000088;">$mp3</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTag</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;album&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Songs from the Big Chair&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// This is essential. We need to write back the changes</span>
<span style="color: #000088;">$mp3</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>If you have duplicate files of the same song, you can also quickly copy tags from one file to the other.</p>

<div class="wp_codebox"><table><tr id="p208014"><td class="code" id="p2080code14"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MP3/Id.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$mp3_1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">&amp;new</span> MP3_Id<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mp3_2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">&amp;new</span> MP3_Id<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$mp3_1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tears For Fears - Shout.mp3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mp3_2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tears For Fears.mp3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Copy tags from 'Tears For Fears - Shout.mp3' to 'Tears For Fears.mp3'</span>
<span style="color: #000088;">$mp3_2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mp3_1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mp3_2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Some other PHP libraries for ID3 manipulation</h4>
<p><a href="http://code.google.com/p/php-reader/wiki/ID3v2" rel="nofollow" >http://code.google.com/p/php-reader/wiki/ID3v2</a><br />
<a href="http://getid3.sourceforge.net/" rel="nofollow" >http://getid3.sourceforge.net/</a></p>
<div  class="download2">
<a href="http://www.codediesel.com/downloads/MP3Scan" rel="nofollow" >Download Source</a><br />
<span>Downloads : 1046  / File size : 925 B</span>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/pear/reading-mp3-file-tags-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Easy manipulation of URLs</title>
		<link>http://www.codediesel.com/pear/easy-manipulation-of-urls-in-php/</link>
		<comments>http://www.codediesel.com/pear/easy-manipulation-of-urls-in-php/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 12:32:21 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[pear]]></category>
		<category><![CDATA[urls]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1868</guid>
		<description><![CDATA[how to manipulate urls using Net_URL2 Pear package]]></description>
			<content:encoded><![CDATA[<p>Whether you are dynamically creating urls or changing existing ones, manipulation of urls is a frequent coding requirement during development; doing the same on short urls is easy, but quickly becomes complex for urls which have larger query parameters.<br />
In this post we will see how we can use <a href="http://pear.php.net/package/Net_URL2/" rel="nofollow" target="_blank" >Net_URL2</a> Pear package to manipulate URLS.<br />
<span id="more-1868"></span></p>
<h4>General url sytnax</h4>
<p>Before we start, a general URL syntax review will be useful. The most general form of a URL contains only two elements:</p>

<div class="wp_codebox"><table><tr id="p186815"><td class="code" id="p1868code15"><pre class="html" style="font-family:monospace;">&lt;scheme&gt;:&lt;scheme-specific-part&gt;</pre></td></tr></table></div>

<p>The term <em>scheme</em> refers to a type of access method such as ftp, http, telnet, file etc; which describes the way the following resource is to be used. The rest of the url, after <em>scheme</em>, is dependent on the scheme type.</p>
<p>A complete generlized syntax for http, ftp is shown below.</p>

<div class="wp_codebox"><table><tr id="p186816"><td class="code" id="p1868code16"><pre class="html" style="font-family:monospace;">&lt;scheme&gt;://&lt;user&gt;:&lt;password&gt;@&lt;host&gt;:&lt;port&gt;/&lt;url-path&gt;;&lt;params&gt;?
&lt;query&gt;#&lt;fragment&gt;</pre></td></tr></table></div>

<h4>Installation</h4>
<p>Net_URL2 being a Pear package we will use the Pear installer as below. I recommend to always use the Pear installer to download packages rather than downloading it manually as the Pear installer automatically downloads any dependent packages.</p>

<div class="wp_codebox"><table><tr id="p186817"><td class="code" id="p1868code17"><pre class="text" style="font-family:monospace;">pear install Net_URL2-0.3.0</pre></td></tr></table></div>

<h4>Reading url data</h4>
<p>Now that we have seen how a general url looks like, its time to move on to real examples. In this example we will use the following sample url.</p>

<div class="wp_codebox"><table><tr id="p186818"><td class="code" id="p1868code18"><pre class="html" style="font-family:monospace;">http://www.some-domain.com:80/search.php?q=beatles&amp;id=56&amp;cat=music</pre></td></tr></table></div>

<p>Below is an example using the Net_URL2 library and its output for the above url:</p>

<div class="wp_codebox"><table><tr id="p186819"><td class="code" id="p1868code19"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Net/URL2.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Net_URL2<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.some-domain.com:80/search.php?
                     q=beatles&amp;id=56&amp;cat=music'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Host      :    &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">host</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Protocol  :    &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">scheme</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Port      :    &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">port</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Path      :    &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Query Variables: <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">QueryVariables</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Which will output the following:</p>

<div class="wp_codebox"><table><tr id="p186820"><td class="code" id="p1868code20"><pre class="text" style="font-family:monospace;">Host      :    www.some-domain.com
Protocol  :    http
Port      :    80
Path      :    /search.php
Query String : 
Array
(
    [q] =&gt; beatles
    [id] =&gt; 56
    [cat] =&gt; music
)</pre></td></tr></table></div>

<h4>Changing url data</h4>
<p>We can as easily change various url parameters as we can read them.</p>

<div class="wp_codebox"><table><tr id="p186821"><td class="code" id="p1868code21"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">protocol</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;https&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">path</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/my_search&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$queryVars</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>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Get the query variables array */</span>
<span style="color: #000088;">$queryVars</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">QueryVariables</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Change some url parameters */</span>
<span style="color: #000088;">$queryVars</span> <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'q'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Scarlett Johansson&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$queryVars</span> <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cat'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;movies&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$queryVars</span> <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pics'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Save back the query variables array */</span>
<span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">QueryVariables</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$queryVars</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Display the changed url */</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">geturl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Which will change the example url to the following:</p>

<div class="wp_codebox"><table><tr id="p186822"><td class="code" id="p1868code22"><pre class="html" style="font-family:monospace;">https://www.some-domain.com:80/New_search.php?
q=Scarlett%20Johansson&amp;id=56&amp;cat=movies&amp;pics=1</pre></td></tr></table></div>

<p>Note the changed parameter values, also note that we have added a new &#8216;pics&#8217; parameter in the url. </p>
<p>We can also change the parameter values using a name,value pair.</p>

<div class="wp_codebox"><table><tr id="p186823"><td class="code" id="p1868code23"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* Change the 'cat' parameter value to 'books' */</span>
<span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setQueryVariable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cat'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;books&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Or unset a parameter</p>

<div class="wp_codebox"><table><tr id="p186824"><td class="code" id="p1868code24"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* This will remove the 'pics' parameter from the url */</span>
<span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">unsetQueryVariable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pics'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can also easily get fragment url identifiers from a url. Fragment identifier locates a sub-location in a resource. If you have a url like the following:</p>

<div class="wp_codebox"><table><tr id="p186825"><td class="code" id="p1868code25"><pre class="html" style="font-family:monospace;">http://www.some-domain.com/index.php#book_id</pre></td></tr></table></div>

<p>The fragment id can be reached by:</p>

<div class="wp_codebox"><table><tr id="p186826"><td class="code" id="p1868code26"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Net_URL2<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.some-domain.com/index.php#book_id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Will return 'book_id' from the url */</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fragment</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If you are accessing a url using some credentials as below:</p>

<div class="wp_codebox"><table><tr id="p186827"><td class="code" id="p1868code27"><pre class="html" style="font-family:monospace;">ftp://username:password@some-domain.com</pre></td></tr></table></div>

<p>You can get the username-password by:</p>

<div class="wp_codebox"><table><tr id="p186828"><td class="code" id="p1868code28"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Username  :    &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Password  :    &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>Normalizing URLS</h4>
<p>URL normalization (or URL canonicalization) is the process by which URLs are modified and standardized in a consistent manner. Normalization helps you determine if two syntactically different URLs are equivalent.</p>
<p>We normalize a url as below.</p>

<div class="wp_codebox"><table><tr id="p186829"><td class="code" id="p1868code29"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Net_URL2<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.example.com/../a/b/../c/./d.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Returns 'http://www.example.com/a/c/d.html' */</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNormalizedURL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>In conclusion</h4>
<p>Net_URL2 package helps you quickly process urls, without resorting to complex regular expressions or string manipulation.</p>
<h4>Additional information</h4>
<p><a href="http://www.ietf.org/rfc/rfc3986.txt" rel="nofollow" >RFC 3986</a><br />
<a href="http://en.wikipedia.org/wiki/Uniform_Resource_Locator" rel="nofollow" >Uniform Resource Locator</a><br />
<a href="http://en.wikipedia.org/wiki/URL_normalization" rel="nofollow" >URL normalization</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/pear/easy-manipulation-of-urls-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Detecting user agents in php</title>
		<link>http://www.codediesel.com/pear/detecting-user-agents-in-php/</link>
		<comments>http://www.codediesel.com/pear/detecting-user-agents-in-php/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 13:48:29 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[browser]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[agents]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1660</guid>
		<description><![CDATA[how to detect browser agents in php]]></description>
			<content:encoded><![CDATA[<p>Every time you use your browser to access a website a User-Agent header is sent to the respective server.<br />
Detecting user agents on the server can be useful for many reasons. </p>
<p>1. Browsers Quirks – Despite the standardization in browsers, there will remain some quirks in various browsers that you will need to iron out on a regular basis.<br />
2. Personalize Content – It may be required to deliver different type of content depending on the browser type (although it is usually not recommended); whether mobile or otherwise.<br />
3. Illegal Access – Prevent bandwidth hogging bots and poorly programmed clients from downloading your content.<br />
<span id="more-1660"></span><br />
Below is a sample header for my site when using Firefox.</p>

<div class="wp_codebox"><table><tr id="p166030"><td class="code" id="p1660code30"><pre class="text" style="font-family:monospace;">http://www.codediesel.com/
&nbsp;
GET / HTTP/1.1
Host: www.codediesel.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6)
Gecko/2009011913 Firefox/3.0.6 FirePHP/0.2.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive</pre></td></tr></table></div>

<p>PHP already has a builtin function &#8211; <a href="http://us2.php.net/manual/en/function.get-browser.php" rel="nofollow" target="_blank" >get_browser()</a> that lets you determine the capabilities of the user&#8217;s browser, but it requires a browscap.ini to be installed, which sadly is not bundled with PHP, and also the function can slow down your php script. The other way to get a user agent is to use php server variables like below,</p>

<div class="wp_codebox"><table><tr id="p166031"><td class="code" id="p1660code31"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_USER_AGENT'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In this post we will use the <a href="http://pear.php.net/package/net_useragent_detect/redirected" rel="nofollow" target="_blank" >Net_UserAgent_Detect</a> Pear package to determine user agents.</p>
<h4>Installation</h4>
<p>Net_UserAgent_Detect being a Pear package we will use the Pear installer as below. I recommend to always use the Pear installer to download packages rather than downloading it manually as the Pear installer automatically downloads any dependent packages.</p>

<div class="wp_codebox"><table><tr id="p166032"><td class="code" id="p1660code32"><pre class="text" style="font-family:monospace;">pear install Net_UserAgent_Detect-2.5.1</pre></td></tr></table></div>

<h4>Usage</h4>
<p>Usage is quite simple. For example the following code:</p>

<div class="wp_codebox"><table><tr id="p166033"><td class="code" id="p1660code33"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'Net/UserAgent/Detect.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> Net_UserAgent_Detect<span style="color: #339933;">::</span><span style="color: #004000;">getUserAgent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>will return the following string on Firefox 3.0.6: This can be quite useful if you need to log the users browser or OS type.</p>

<div class="wp_codebox"><table><tr id="p166034"><td class="code" id="p1660code34"><pre class="text" style="font-family:monospace;">Mozilla/5.0 (Windows; U; Windows NT 5.1; 
en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6 FirePHP/0.2.4</pre></td></tr></table></div>

<p>getUserAgent() is only one of the many methods available in the class.</p>

<div class="wp_codebox"><table><tr id="p166035"><td class="code" id="p1660code35"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'Net/UserAgent/Detect.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Browser String: '</span><span style="color: #339933;">.</span>Net_UserAgent_Detect<span style="color: #339933;">::</span><span style="color: #004000;">getBrowserString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'OS String: '</span><span style="color: #339933;">.</span>Net_UserAgent_Detect<span style="color: #339933;">::</span><span style="color: #004000;">getOSString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Javascript: '</span><span style="color: #339933;">.</span>Net_UserAgent_Detect<span style="color: #339933;">::</span><span style="color: #004000;">getFeature</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'javascript'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Some other useful functions to directly test the type of browser are:<br />
isIE()<br />
isNavigator( )<br />
isNetscape( )</p>
<p>So to detect if the user is running Firefox:</p>

<div class="wp_codebox"><table><tr id="p166036"><td class="code" id="p1660code36"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$browser</span> <span style="color: #339933;">=</span> Net_UserAgent_Detect<span style="color: #339933;">::</span><span style="color: #004000;">isNetscape</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/pear/detecting-user-agents-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Beautifying XML documents</title>
		<link>http://www.codediesel.com/pear/beautifying-xml-documents/</link>
		<comments>http://www.codediesel.com/pear/beautifying-xml-documents/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 04:48:52 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1590</guid>
		<description><![CDATA[how to beautify and format xml documents]]></description>
			<content:encoded><![CDATA[<p>I frequently write php code to access various web services and the most common response data I encounter is in xml, which most of the time is not formatted. I use <a href="http://www.wmhelp.com/" rel="nofollow" target="_blank" >xmlPad</a> to format and analyze xml documents, but many times I need to format xml documents on the production server wherein xmlPad is of no use. What one needs is a library that would allow you to beautify your untidy xml documents within your php code. <a href="http://pear.php.net/manual/en/package.xml.xml-beautifier.php" rel="nofollow" target="_blank" >XML_Beautifier</a> provides that solution.<br />
<span id="more-1590"></span></p>
<h4>Installation</h4>
<p>XML_Beautifier being a Pear package we will use the Pear installer as below. I recommend to always use the Pear installer to download packages rather than dowloading it manually as the Pear installer automatically downloads any dependent packages.</p>

<div class="wp_codebox"><table><tr id="p159037"><td class="code" id="p1590code37"><pre class="text" style="font-family:monospace;">pear install XML_Beautifier</pre></td></tr></table></div>

<h4>Usage</h4>
<p>You can beautify a xml file or a on-the-fly generated xml string. The following will nicely format a untidy xml file. The input file must be a valid xml document or the parser will flag an error.</p>

<div class="wp_codebox"><table><tr id="p159038"><td class="code" id="p1590code38"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">&quot;XML/Beautifier.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fmt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XML_Beautifier<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$fmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">formatFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'unformated.xml'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'beautified.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>PEAR<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Done&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Formatting a xml string is quite as simple.</p>

<div class="wp_codebox"><table><tr id="p159039"><td class="code" id="p1590code39"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">&quot;XML/Beautifier.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Unformatted xml string */</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;rootNode&gt;&lt;foo   bar = &quot;pear&quot;&gt;hello world!&lt;/foo&gt;&lt;/rootNode&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fmt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XML_Beautifier<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$fmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">formatString</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Formatted xml string.</p>

<div class="wp_codebox"><table><tr id="p159040"><td class="code" id="p1590code40"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rootNode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;foo</span> <span style="color: #000066;">bar</span>=<span style="color: #ff0000;">&quot;pear&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>hello world!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/foo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rootNode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>If you frequently need to format xml documents than it would be better to have a command line access to the beautifier as below.</p>

<div class="wp_codebox"><table><tr id="p159041"><td class="code" id="p1590code41"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/** xmlformat.php
 *  usage: php xmlformat.php untidy.xml beautified.xml
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$argc</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Usage: <span style="color: #006699; font-weight: bold;">$argv[0]</span> unformatted.xml formatted.xml<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">&quot;XML/Beautifier.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Formatting <span style="color: #006699; font-weight: bold;">{$argv[1]}</span>...&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$fmt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XML_Beautifier<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$fmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">formatFile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$argv</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: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>PEAR<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Done&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Options</h4>
<p>The <em>XML_Beautifier</em> class also accepts a array of options which you can pass to the constructor during initialization. The details of the options can be found <a href="http://pear.php.net/manual/en/package.xml.xml-beautifier.options.php" rel="nofollow" target="_blank" >here</a>.</p>

<div class="wp_codebox"><table><tr id="p159042"><td class="code" id="p1590code42"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #0000ff;">&quot;caseFolding&quot;</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">&quot;caseFoldingTo&quot;</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;uppercase&quot;</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">&quot;normalizeComments&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fmt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XML_Beautifier<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>One thing to note in closing is that the formatting of big xml documents can take some time depending on your machine configuration, so if you plan on formatting documents on a live server you need to take that into account.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/pear/beautifying-xml-documents/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Search &amp; replace in files using php</title>
		<link>http://www.codediesel.com/php/search-replace-in-files-using-php/</link>
		<comments>http://www.codediesel.com/php/search-replace-in-files-using-php/#comments</comments>
		<pubDate>Mon, 11 May 2009 13:57:51 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=819</guid>
		<description><![CDATA[Searching and replacing content in files is a common task all of us do regularly. Most programmers will implement it using Perl a shell script or through a editor. Perl offers itself as an excellent tool for the required purpose; we PHP programmers are not quite so lucky in that matter. Search/replace is easier from [...]]]></description>
			<content:encoded><![CDATA[<p>Searching and replacing content in files is a common task all of us do regularly. Most programmers will implement it using Perl a shell script or through a editor. Perl offers itself as an excellent tool for the required purpose; we PHP programmers are not quite so lucky in that matter. Search/replace is easier from a shell prompt or an editor, but what if you have to do the same programatically in php. <a href="http://pear.php.net/package/File_SearchReplace/download" rel="nofollow" target="_blank" >File_SearchReplace</a> is a pear package that helps you search/replace in files through a nice object oriented interface.<br />
<span id="more-819"></span><br />
<strong>Installation</strong><br />
Pear installation as usual is simple.</p>

<div class="wp_codebox"><table><tr id="p81949"><td class="code" id="p819code49"><pre class="dos" style="font-family:monospace;">c:/&gt; pear install File_SearchReplace</pre></td></tr></table></div>

<p><strong>Doing a simple search &#038; replace</strong><br />
The following is an simple example code that searches the file &#8216;fruits.txt&#8217; and replaces all occurrences of &#8216;apples&#8217; with &#8216;oranges&#8217;. The <em>getNumOccurences</em> function returns the total number of replaced strings in the file.</p>

<div class="wp_codebox"><table><tr id="p81950"><td class="code" id="p819code50"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">'File/SearchReplace.php'</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$files_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fruits.txt&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$search_string</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;apples&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;oranges&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$replace_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$files_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #009933; font-style: italic;">// directorie(s) to search</span>
                              <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The fourth option in the <em>File_SearchReplace</em> specifies a optional directory name to search. If the directory name is empty than the files will be searched in the current directory or in the respective path if a path is also included with the filename. Following is an example if you want to search all files in the directory &#8216;nature/fruits&#8217;.</p>

<div class="wp_codebox"><table><tr id="p81951"><td class="code" id="p819code51"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">'File/SearchReplace.php'</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$files_to_search</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: #000088;">$direc_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nature/fruits/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$search_string</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;apples&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;oranges&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$replace_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$files_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$direc_to_search</span><span style="color: #339933;">,</span> <span style="color: #009933; font-style: italic;">// directory to search</span>
                              <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// 'true' to search subdirectories</span>
&nbsp;
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>All the four starting options of <em>File_SearchReplace</em> are of mixed type; i.e they take a string or an array of strings as their options. For example in the following all occurrences of &#8216;apples&#8217; will be replaced by &#8216;oranges&#8217; and that of &#8216;pears&#8217; by &#8216;grapes&#8217;.</p>

<div class="wp_codebox"><table><tr id="p81952"><td class="code" id="p819code52"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">'File/SearchReplace.php'</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$files_to_search</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: #000088;">$direc_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nature/fruits/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$search_string</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'apples'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pears'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace_string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'oranges'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'grapes'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$replace_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$files_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$direc_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>You don&#8217;t have to create a new instance everytime you need a new search, you can set the various parameters through the interface provided by the <em>File_SearchReplace</em> class as shown below.</p>

<div class="wp_codebox"><table><tr id="p81953"><td class="code" id="p819code53"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">'File/SearchReplace.php'</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$files_to_search</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: #000088;">$direc_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nature/fruits/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$search_string</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'apples'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pears'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace_string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'oranges'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'grapes'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$replace_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$files_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$direc_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Start a new search */</span>
&nbsp;
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFind</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;oranges&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setReplace</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;berries&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Regular Expression search</strong><br />
You can also use a regular expression in a search string, but before that we must specify what kind of search is required with the &#8216;setSearchFunction&#8217; as shown below. The following example replaces all occurrences of &#8216;color&#8217; or &#8216;colour&#8217; with the capital &#8216;COLOR&#8217;.</p>

<div class="wp_codebox"><table><tr id="p81954"><td class="code" id="p819code54"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">'File/SearchReplace.php'</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$files_to_search</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: #000088;">$direc_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test/graphics/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$search_string</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/col(o|ou)r/'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'COLOR'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$replace_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$files_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$direc_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSearchFunction</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;preg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The <em>setSearchFunction</em> takes one of the following four options:</p>
<p>normal &#8211; default<br />
quick &#8211; use str_replace()<br />
preg &#8211; use preg_replace()<br />
ereg &#8211; use ereg_replace()</p>
<p>More information on this options can be found <a href="http://pear.php.net/manual/en/package.filesystem.file-searchreplace.intro.php" rel="nofollow" target="_blank" >here</a>.</p>
<p><strong>In conclusion</strong><br />
The package can be quite useful when you want to replace large quantities of text programatically. As it works on plain strings and regular expressions, its can be quite a handy tool in many occasions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/search-replace-in-files-using-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Simple Pagination in PHP tutorial</title>
		<link>http://www.codediesel.com/php/simple-pagination-in-php/</link>
		<comments>http://www.codediesel.com/php/simple-pagination-in-php/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 05:03:48 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[pear]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=118</guid>
		<description><![CDATA[PHP pagination tutorial using the pears pager library]]></description>
			<content:encoded><![CDATA[<p>Pagination is a frequent requirement in web development projects. Most PHP developers must have already implementated paging in one form or other in their projects. In this post we will see how to add pagination the easy way using PEAR&#8217;s Pager class. Note that in all the posts I use PHP 5.x.x, so if you are still stuck at version 4.x.x, its already time to upgrade.<br />
<span id="more-118"></span></p>
<p>Its always nice to see some working code first, before getting into the details. So here goes.</p>

<div class="wp_codebox"><table><tr id="p11855"><td class="code" id="p118code55"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'Pager/Pager.php'</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/* We will bypass the database connection code ... */</span>
<span style="color: #000088;">$sqlQuery</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SOME SQL QUERY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlQuery</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$totalRows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalRows</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pager</span> <span style="color: #339933;">=</span> Pager<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pager_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The above code will return the following pagination links.</p>
<p>1   |   2   |   3   |   4   |   5   |   6   |   7   |   8   |   9      »      [10]</p>
<p>Assuming the page where the above code is included is named &#8216;dataPage.php&#8217;; the pager links will have a url like follows:</p>
<p>dataPage.php?pageID=<em>n</em></p>
<p>where <em>n</em> is the page number.</p>
<p>You can see how easy it is using the Pager class. All the link building is handled by the class. Now lets go into the details.</p>
<h4>1. Installation</h4>
<p>If you are installing using the Pear installer, enter the following at the command prompt to install Pager.</p>

<div class="wp_codebox"><table><tr id="p11856"><td class="code" id="p118code56"><pre class="php" style="font-family:monospace;">c<span style="color: #339933;">:</span>\pear install Pager</pre></td></tr></table></div>

<p>Or else you can just download the module from <a href="http://pear.php.net/package/Pager/download" rel="nofollow" >here</a> and then copy it to your Pear includes directory, which will probably be &#8216;c:\<em>your php installation directory</em>\Pear&#8217;.</p>
<h4>2. A simple example</h4>
<p>In the following example we will create a pagination for a &#8216;logs&#8217; table.</p>

<div class="wp_codebox"><table><tr id="p11857"><td class="code" id="p118code57"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* Include the Pear::Pager file */</span>
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Pager/Pager.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Replace this with your database details */</span>
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> username<span style="color: #339933;">,</span> password<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span>database name<span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* First we need to get the total rows in the table */</span>
<span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT count(*) AS total FROM logs&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Total number of rows in the logs table */</span>
<span style="color: #000088;">$totalItems</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'total'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Set some options for the Pager */</span>
<span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>   <span style="color: #009933; font-style: italic;">// Sliding or Jumping mode. See below.</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>   <span style="color: #009933; font-style: italic;">// Total rows to show per page</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>   <span style="color: #009933; font-style: italic;">// See below</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalItems</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Initialize the Pager class with the above options */</span>
<span style="color: #000088;">$pager</span> <span style="color: #339933;">=</span> Pager<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pager_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Display the links */</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* The following code will retreive the result using the pager options */</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* The function below will get the page offsets to be used with
the database query. For e.g if we are on the third page then the
$from variable will have the value of '21' (we are showing 10 items per 
page, remember) and the $to variable will have the value of '30'.
*/</span>
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$from</span><span style="color: #339933;">,</span> <span style="color: #000088;">$to</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOffsetByPageId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/* The MySQL 'LIMIT' clause index starts from '0', 
    so decrease the $from by 1 */</span>
<span style="color: #000088;">$from</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$from</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* The number of rows to get per query */</span>
<span style="color: #000088;">$perPage</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pager_options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'perPage'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM alogs LIMIT <span style="color: #006699; font-weight: bold;">$from</span> , <span style="color: #006699; font-weight: bold;">$perPage</span>&quot;</span><span style="color: #339933;">,</span>
<span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009933; font-style: italic;">/* Do something with the query results */</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In the Pager class there are two modes to display the pagination: Sliding &amp; Jumping.</p>
<p>With Pager in &#8220;Sliding&#8221; mode the pagination links change smoothly, and the current page is always shown at the center of the &#8220;window&#8221; (except for the start and end pages). When in Sliding mode the delta option implies how many links to show on the left and right of the current page link.<br />
For e.g with a delta set to &#8217;3&#8242; the links are displayed as shown below, page 10 being the current page:</p>
<p>[1]      «      7   |   8   |   9   |   <strong>10</strong> |   11   |   12   |   13      »      [13]</p>
<p>With delta option set to &#8217;4&#8242; the links are shown as below. As you can see there are four links to the left and right of the current page.</p>
<p>[1]      «      5   |   6   |   7   |   8   |   <strong>9</strong> |   10   |   11   |   12   |   13      »      [13]</p>
<p>With the Pager in &#8220;Jumping&#8221; mode and delta set to &#8217;4&#8242; the Pager always shows the same 4 page links while you are on one of these pages.<br />
For e.g with a delta set to &#8217;4&#8242; the links are displayed as shown below, page 5 being the current page:</p>
<p>[1] &lt;&lt; Back <strong>5</strong> 6 7 8  Next &gt;&gt; [13]</p>
<p>If you are on page &#8217;9&#8242; the links displayed are as below:</p>
<p>[1] &lt;&lt; Back <strong>9</strong> 10 11 12  Next &gt;&gt; [13]</p>
<h4>3. More options</h4>
<p>You can also change the default separator to any character or image you like.</p>

<div class="wp_codebox"><table><tr id="p11858"><td class="code" id="p118code58"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'separator'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">','</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalItems</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will output the following:<br />
[1]      «      1   ,   2   ,   3   ,   4   ,   5   ,   6   ,   7   ,   8   ,   9      »      [13]</p>
<p>If you would like to add images to the &#8216;next&#8217; and &#8216;previous&#8217; links, use the following options:</p>

<div class="wp_codebox"><table><tr id="p11859"><td class="code" id="p118code59"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'nextImg'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;img src=&quot;next.png&quot;  /&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'prevImg'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;img src=&quot;prev.png&quot;  /&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalItems</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Which will display the links as shown below:<br />
<a href="http://www.codediesel.com/wp-content/uploads/2008/10/pager2.gif"><img class="aligncenter size-full wp-image-121" style="border:1px solid #c0c0c0;" title="pager2" src="http://www.codediesel.com/wp-content/uploads/2008/10/pager2.gif" alt="" width="472" height="32" /></a></p>
<p>The default number of spaces before each separator is three. To change the number of spaces use the &#8216;spacesBeforeSeparator&#8217; option. For e.g:</p>

<div class="wp_codebox"><table><tr id="p11860"><td class="code" id="p118code60"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #0000ff;">'spacesBeforeSeparator'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<h4>4. Styling</h4>
<p>You can style the links by putting them in a div tag and applying a css. A example css and its output is shown below.</p>
<p>Add a div tag:</p>

<div class="wp_codebox"><table><tr id="p11861"><td class="code" id="p118code61"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">'&lt;div class=&quot;pager&quot;&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Apply CSS:</p>

<div class="wp_codebox"><table><tr id="p11862"><td class="code" id="p118code62"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.pager</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">14px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.pager</span> a <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">14px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">17px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">17px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#0C74BA</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.pager</span> a<span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#c0c0c0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>The output:<br />
<a href="http://www.codediesel.com/wp-content/uploads/2008/10/pager.gif"><img class="aligncenter size-full wp-image-119" style="border:1px solid #c0c0c0;" title="pager" src="http://www.codediesel.com/wp-content/uploads/2008/10/pager.gif" alt="pager" width="376" height="35" /></a></p>
<p>That&#8217;s it. There are many other options you can use with the Pager class, the details which you can find in the Pager <a href="http://pear.php.net/manual/en/package.html.pager.php" rel="nofollow"  target="_blank">documentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/simple-pagination-in-php/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
	</channel>
</rss>

