<?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>Codes Tips</title>
	<atom:link href="http://codestips.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://codestips.com</link>
	<description>Tips &#38; Tutorials for everyone who loves programming</description>
	<lastBuildDate>Sat, 29 Oct 2011 19:47:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PHP pdf generator from html</title>
		<link>http://codestips.com/php-pdf-generator-from-html/</link>
		<comments>http://codestips.com/php-pdf-generator-from-html/#comments</comments>
		<pubDate>Sat, 29 Oct 2011 19:44:37 +0000</pubDate>
		<dc:creator>Codes Tips</dc:creator>
				<category><![CDATA[Codes]]></category>
		<category><![CDATA[PHP/MYSQL]]></category>
		<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://codestips.com/?p=373</guid>
		<description><![CDATA[I will explain in this article how to make a pdf generator from html in php. We will use a command tool that will generate the pdf. It&#8217;s an open source shell utility that is called wkhtmltopdf that uses webkit rendering engine, and qt &#8211; go here for more information for wkhtmltopdf. I suggest to [...]]]></description>
			<content:encoded><![CDATA[<span class="fb_share"><fb:like href="http://codestips.com/php-pdf-generator-from-html/" layout="box_count"></fb:like></span><fb:like href='http%3A%2F%2Fcodestips.com%2Fphp-pdf-generator-from-html%2F' send='true' layout='button_count' show_faces='true' width='450' height='65' action='recommend' colorscheme='light' font='lucida+grande'></fb:like><p>I will explain in this article how to make a <strong>pdf generator from html</strong> in php.<br />
We will use a command tool that will generate the pdf. It&#8217;s an open source shell utility that is called <em>wkhtmltopdf</em> that uses webkit rendering engine, and qt &#8211; go here for more information for <a title="wkhtmltopdf" href="http://code.google.com/p/wkhtmltopdf/" target="_blank">wkhtmltopdf</a>. I suggest to use the version 0.9.9, which I think it&#8217;s a stable version right now. I tested the version 0.11.0_rc1 and I got some problems with downloading the images from html img tag.<br />
Steps to generate the pdf from html:<br />
1. Download the tool.<br />
2. Install the tool.<br />
3. Use the tool from your php code to create a pdf from html.</p>
<p>1. Depending on your operating system you should download from the following page:</p>
<ul>
<li><a title="Windows" href="http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltopdf-0.9.9-installer.exe" target="_blank">Windows</a></li>
<li><a title="Linux AMD" href="http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltopdf-0.9.9-static-amd64.tar.bz2" target="_blank">Linux AMD</a> or <a title="Linux i368" href="http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltopdf-0.9.9-static-i386.tar.bz2" target="_blank">Linux i368</a></li>
</ul>
<p>2. To install on Windows should download the .exe from the website, launch the setup and follow the steps. In order to test if you installed correctly the tool you should go to the folder of where you installed it and launch in command prompt the following command: <strong>wkhtmltopdf http://www.test.com test.pdf<br />
</strong><br />
To install on Linux you should find out the operating system launching the command <strong>uname -a</strong> that will tell you the operating system you are using and download the apropiate version of the tool.<br />
To install on 32 bits Linux operating system you should use the following code:<br />
<code><br />
//get the file from server<br />
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-i386.tar.bz2<br />
tar xvjf wkhtmltopdf-0.9.9-static-i386.tar.bz2<br />
// move the application to bin folder<br />
mv wkhtmltopdf-i386 /usr/local/bin/wkhtmltopdf<br />
//change permission for install it<br />
chmod +x /usr/local/bin/wkhtmltopdf<br />
// install it<br />
/usr/local/bin/wkhtmltopdf<br />
//To test if you installed correctly the tool<br />
wkhtmltopdf-i386 http://www.test.com test.pdf<br />
</code></p>
<p>To install on 64 bits Linux operating system you should use the following code:<br />
<code><br />
//get the file from server<br />
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2<br />
tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2<br />
// move the application to bin folder<br />
mv wkhtmltopdf-amd64/usr/local/bin/wkhtmltopdf<br />
//change permission for install it<br />
chmod +x /usr/local/bin/wkhtmltopdf<br />
// install it<br />
/usr/local/bin/wkhtmltopdf<br />
//To test if you installed correctly the tool<br />
wkhtmltopdf-amd64 http://www.test.com test.pdf<br />
</code></p>
<p>3. In php you should execute the tool using <strong>shell_exec</strong>:<br />
<code><br />
shell_exec("/usr/local/bin/wkhtmltopdf-i386 http://test.com test.pdf");<br />
//for Linux 32 bits operating system<br />
//for Linux 64 bits operating system use wkhtmltopdf-amd64<br />
//for windows just put the path of the exe file.<br />
$allfile = file_get_contents("test.pdf");<br />
header('Content-Type: application/pdf');<br />
header('Content-Length: '.strlen($allfile));<br />
header('Content-Disposition: inline; filename="test.pdf"');<br />
header('Cache-Control: private, max-age=0, must-revalidate');<br />
header('Pragma: public');<br />
ini_set('zlib.output_compression','0');<br />
</code></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://blinklist.com/index.php?Action=Blink/addblink.php&amp;Name=PHP+pdf+generator+from+html&amp;Description=PHP+pdf+generator+from+html&amp;Url=http://codestips.com/php-pdf-generator-from-html/" rel="nofollow" title="Add to&nbsp;BlinkList"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/blinklist.png" title="Add to&nbsp;BlinkList" alt="Add to&nbsp;BlinkList" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://codestips.com/php-pdf-generator-from-html/&amp;title=PHP+pdf+generator+from+html" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://codestips.com/php-pdf-generator-from-html/&amp;title=PHP+pdf+generator+from+html" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=PHP+pdf+generator+from+html&amp;url=http://codestips.com/php-pdf-generator-from-html/&amp;title=PHP+pdf+generator+from+html" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://codestips.com/php-pdf-generator-from-html/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=PHP+pdf+generator+from+html&amp;u=http://codestips.com/php-pdf-generator-from-html/" rel="nofollow" title="Add to&nbsp;FURL"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/furl.png" title="Add to&nbsp;FURL" alt="Add to&nbsp;FURL" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://codestips.com/php-pdf-generator-from-html/&amp;title=PHP+pdf+generator+from+html" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.kaboodle.com/za/selectpage?p_pop=false&amp;pa=url&amp;u=http://codestips.com/php-pdf-generator-from-html/" rel="nofollow" title="Add to&nbsp;Kaboodle"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/kaboodle.png" title="Add to&nbsp;Kaboodle" alt="Add to&nbsp;Kaboodle" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http://codestips.com/php-pdf-generator-from-html/&amp;T=PHP+pdf+generator+from+html" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://netvouz.com/action/submitBookmark?url=http://codestips.com/php-pdf-generator-from-html/&amp;title=PHP+pdf+generator+from+html&amp;popup=no" rel="nofollow" title="Add to&nbsp;Netvouz"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netvouz.png" title="Add to&nbsp;Netvouz" alt="Add to&nbsp;Netvouz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://codestips.com/php-pdf-generator-from-html/&amp;title=PHP+pdf+generator+from+html" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.simpy.com/simpy/LinkAdd.do?href=http://codestips.com/php-pdf-generator-from-html/&amp;title=PHP+pdf+generator+from+html" rel="nofollow" title="Add to&nbsp;Simpy"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/simpy.png" title="Add to&nbsp;Simpy" alt="Add to&nbsp;Simpy" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://codestips.com/php-pdf-generator-from-html/&amp;title=PHP+pdf+generator+from+html" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.squidoo.com/lensmaster/bookmark?http://codestips.com/php-pdf-generator-from-html/" rel="nofollow" title="Add to&nbsp;Squidoo"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/squidoo.png" title="Add to&nbsp;Squidoo" alt="Add to&nbsp;Squidoo" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.spurl.net/spurl.php?url=http://codestips.com/php-pdf-generator-from-html/&amp;title=PHP+pdf+generator+from+html" rel="nofollow" title="Add to&nbsp;Spurl"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/spurl.png" title="Add to&nbsp;Spurl" alt="Add to&nbsp;Spurl" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://codestips.com/php-pdf-generator-from-html/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://codestips.com/php-pdf-generator-from-html/&amp;t=PHP+pdf+generator+from+html" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://codestips.com/php-pdf-generator-from-html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP database logging</title>
		<link>http://codestips.com/php-database-logging/</link>
		<comments>http://codestips.com/php-database-logging/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 23:34:02 +0000</pubDate>
		<dc:creator>Codes Tips</dc:creator>
				<category><![CDATA[Codes]]></category>
		<category><![CDATA[PHP/MYSQL]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://codestips.com/?p=366</guid>
		<description><![CDATA[In order to make database logging in php we should first create a table with 2 fields: the message that we log and the date that the log was made. After creating the database we should create a php class that will have to pass the instance of the MySQL database to the log class [...]]]></description>
			<content:encoded><![CDATA[<span class="fb_share"><fb:like href="http://codestips.com/php-database-logging/" layout="box_count"></fb:like></span><fb:like href='http%3A%2F%2Fcodestips.com%2Fphp-database-logging%2F' send='true' layout='button_count' show_faces='true' width='450' height='65' action='recommend' colorscheme='light' font='lucida+grande'></fb:like><p>In order to make <strong>database logging in php</strong> we should first create a table with 2 fields: the message that we log and the date that the log was made. After creating the database we should create a php class that will have to pass the instance of the MySQL database to the log class contructor. After that as previous example of php log file we should call a function that inserts a new row in a table database. Here is the class for <b>php database logging</b>:</p>
<p><code><br />
class DBLog {<br />
  private $_db; </p>
<p>  public function __construct($db) {<br />
   	$this->_db = $db;<br />
  } </p>
<p>  public function WriteLogToDB($message) {<br />
	if(!empty($message)){<br />
		$date = date("Y-m-d h:m:s");<br />
		mysql_query ("INSERT INTO DBLog VALUES('".$message."', '$date')", $this->_db);<br />
	}<br />
  }<br />
}<br />
</code></p>
<p>In order to call the function of database logging we should first make a connection to the database and then call the function to save row log to database:</p>
<p><code><br />
$connection = mysql_connect("localhost", "username", "password")<br />
	or die("Unable to connect to  MySQL server");<br />
mysql_select_db('DatabaseName', $connection) or die ("Database not found.");<br />
$filel = new DBLog($connection);<br />
$filel->WriteLogToDB('Here is my first log.');<br />
</code></p>
<p>We only need the mysql script that creates the table in our database:</p>
<p><code><br />
CREATE TABLE IF NOT EXISTS `dblog` (<br />
  `Message` text NOT NULL,<br />
  `DateCreated` datetime NOT NULL<br />
) ENGINE=MyISAM DEFAULT CHARSET=utf8;<br />
</code></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://blinklist.com/index.php?Action=Blink/addblink.php&amp;Name=PHP+database+logging&amp;Description=PHP+database+logging&amp;Url=http://codestips.com/php-database-logging/" rel="nofollow" title="Add to&nbsp;BlinkList"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/blinklist.png" title="Add to&nbsp;BlinkList" alt="Add to&nbsp;BlinkList" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://codestips.com/php-database-logging/&amp;title=PHP+database+logging" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://codestips.com/php-database-logging/&amp;title=PHP+database+logging" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=PHP+database+logging&amp;url=http://codestips.com/php-database-logging/&amp;title=PHP+database+logging" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://codestips.com/php-database-logging/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=PHP+database+logging&amp;u=http://codestips.com/php-database-logging/" rel="nofollow" title="Add to&nbsp;FURL"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/furl.png" title="Add to&nbsp;FURL" alt="Add to&nbsp;FURL" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://codestips.com/php-database-logging/&amp;title=PHP+database+logging" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.kaboodle.com/za/selectpage?p_pop=false&amp;pa=url&amp;u=http://codestips.com/php-database-logging/" rel="nofollow" title="Add to&nbsp;Kaboodle"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/kaboodle.png" title="Add to&nbsp;Kaboodle" alt="Add to&nbsp;Kaboodle" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http://codestips.com/php-database-logging/&amp;T=PHP+database+logging" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://netvouz.com/action/submitBookmark?url=http://codestips.com/php-database-logging/&amp;title=PHP+database+logging&amp;popup=no" rel="nofollow" title="Add to&nbsp;Netvouz"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netvouz.png" title="Add to&nbsp;Netvouz" alt="Add to&nbsp;Netvouz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://codestips.com/php-database-logging/&amp;title=PHP+database+logging" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.simpy.com/simpy/LinkAdd.do?href=http://codestips.com/php-database-logging/&amp;title=PHP+database+logging" rel="nofollow" title="Add to&nbsp;Simpy"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/simpy.png" title="Add to&nbsp;Simpy" alt="Add to&nbsp;Simpy" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://codestips.com/php-database-logging/&amp;title=PHP+database+logging" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.squidoo.com/lensmaster/bookmark?http://codestips.com/php-database-logging/" rel="nofollow" title="Add to&nbsp;Squidoo"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/squidoo.png" title="Add to&nbsp;Squidoo" alt="Add to&nbsp;Squidoo" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.spurl.net/spurl.php?url=http://codestips.com/php-database-logging/&amp;title=PHP+database+logging" rel="nofollow" title="Add to&nbsp;Spurl"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/spurl.png" title="Add to&nbsp;Spurl" alt="Add to&nbsp;Spurl" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://codestips.com/php-database-logging/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://codestips.com/php-database-logging/&amp;t=PHP+database+logging" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://codestips.com/php-database-logging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP file log</title>
		<link>http://codestips.com/php-file-log/</link>
		<comments>http://codestips.com/php-file-log/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 19:36:53 +0000</pubDate>
		<dc:creator>Codes Tips</dc:creator>
				<category><![CDATA[PHP/MYSQL]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[log]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://codestips.com/?p=358</guid>
		<description><![CDATA[Writing a file log on disk with php it&#8217;s pretty straigt forward. Here is a simple php class that can help you log to a file. The class is logging on the directory where the script exists. You can also create a directory where the script inside and change the path in order to log [...]]]></description>
			<content:encoded><![CDATA[<span class="fb_share"><fb:like href="http://codestips.com/php-file-log/" layout="box_count"></fb:like></span><fb:like href='http%3A%2F%2Fcodestips.com%2Fphp-file-log%2F' send='true' layout='button_count' show_faces='true' width='450' height='65' action='recommend' colorscheme='light' font='lucida+grande'></fb:like><p>Writing a file log on disk with php it&#8217;s pretty straigt forward. Here is a simple php class that can help you log to a file. The class is logging on the directory where the script exists. You can also create a directory where the script inside and change the path in order to log to the directory you created. In the constructor of the class you should pass the name of the log file. Every time you call the function <strong>WriteLogToFile</strong> it appends messages in the file log of the directory script.</p>
<p><code><br />
<?php<br />
class FileLogger{<br />
private $filename;<br />
public function __construct($file) {<br />
$this-&gt;filename = $file;<br />
}</p>
<p>public function WriteLogToFile($messageLog) {<br />
$currentDir = dirname(__FILE__)."/";<br />
$fileLog = fopen($currentDir.$this-&gt;filename, 'a+');<br />
fwrite($fileLog, $messageLog."\r\n");<br />
fclose($fileLog);<br />
}<br />
}<br />
?><br />
</code></p>
<p>Calling function example:<br />
<code>$filel = new FileLogger('file.log');<br />
$filel-&gt;WriteLogToFile('log...');</code></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://blinklist.com/index.php?Action=Blink/addblink.php&amp;Name=PHP+file+log&amp;Description=PHP+file+log&amp;Url=http://codestips.com/php-file-log/" rel="nofollow" title="Add to&nbsp;BlinkList"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/blinklist.png" title="Add to&nbsp;BlinkList" alt="Add to&nbsp;BlinkList" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://codestips.com/php-file-log/&amp;title=PHP+file+log" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://codestips.com/php-file-log/&amp;title=PHP+file+log" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=PHP+file+log&amp;url=http://codestips.com/php-file-log/&amp;title=PHP+file+log" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://codestips.com/php-file-log/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=PHP+file+log&amp;u=http://codestips.com/php-file-log/" rel="nofollow" title="Add to&nbsp;FURL"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/furl.png" title="Add to&nbsp;FURL" alt="Add to&nbsp;FURL" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://codestips.com/php-file-log/&amp;title=PHP+file+log" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.kaboodle.com/za/selectpage?p_pop=false&amp;pa=url&amp;u=http://codestips.com/php-file-log/" rel="nofollow" title="Add to&nbsp;Kaboodle"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/kaboodle.png" title="Add to&nbsp;Kaboodle" alt="Add to&nbsp;Kaboodle" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http://codestips.com/php-file-log/&amp;T=PHP+file+log" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://netvouz.com/action/submitBookmark?url=http://codestips.com/php-file-log/&amp;title=PHP+file+log&amp;popup=no" rel="nofollow" title="Add to&nbsp;Netvouz"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netvouz.png" title="Add to&nbsp;Netvouz" alt="Add to&nbsp;Netvouz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://codestips.com/php-file-log/&amp;title=PHP+file+log" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.simpy.com/simpy/LinkAdd.do?href=http://codestips.com/php-file-log/&amp;title=PHP+file+log" rel="nofollow" title="Add to&nbsp;Simpy"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/simpy.png" title="Add to&nbsp;Simpy" alt="Add to&nbsp;Simpy" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://codestips.com/php-file-log/&amp;title=PHP+file+log" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.squidoo.com/lensmaster/bookmark?http://codestips.com/php-file-log/" rel="nofollow" title="Add to&nbsp;Squidoo"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/squidoo.png" title="Add to&nbsp;Squidoo" alt="Add to&nbsp;Squidoo" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.spurl.net/spurl.php?url=http://codestips.com/php-file-log/&amp;title=PHP+file+log" rel="nofollow" title="Add to&nbsp;Spurl"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/spurl.png" title="Add to&nbsp;Spurl" alt="Add to&nbsp;Spurl" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://codestips.com/php-file-log/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://codestips.com/php-file-log/&amp;t=PHP+file+log" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://codestips.com/php-file-log/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JQuery posts</title>
		<link>http://codestips.com/jquery-posts/</link>
		<comments>http://codestips.com/jquery-posts/#comments</comments>
		<pubDate>Fri, 26 Aug 2011 22:03:59 +0000</pubDate>
		<dc:creator>Codes Tips</dc:creator>
				<category><![CDATA[Announcements]]></category>

		<guid isPermaLink="false">http://codestips.com/?p=355</guid>
		<description><![CDATA[Hi, It was some time that I didn&#8217;t wrote on codestips. I want to start some posts about JQuery and Facebook development. Bookmark It]]></description>
			<content:encoded><![CDATA[<span class="fb_share"><fb:like href="http://codestips.com/jquery-posts/" layout="box_count"></fb:like></span><fb:like href='http%3A%2F%2Fcodestips.com%2Fjquery-posts%2F' send='true' layout='button_count' show_faces='true' width='450' height='65' action='recommend' colorscheme='light' font='lucida+grande'></fb:like><p>Hi,</p>
<p>It was some time that I didn&#8217;t wrote on codestips. I want to start some posts about JQuery and Facebook development.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://blinklist.com/index.php?Action=Blink/addblink.php&amp;Name=JQuery+posts&amp;Description=JQuery+posts&amp;Url=http://codestips.com/jquery-posts/" rel="nofollow" title="Add to&nbsp;BlinkList"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/blinklist.png" title="Add to&nbsp;BlinkList" alt="Add to&nbsp;BlinkList" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://codestips.com/jquery-posts/&amp;title=JQuery+posts" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://codestips.com/jquery-posts/&amp;title=JQuery+posts" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=JQuery+posts&amp;url=http://codestips.com/jquery-posts/&amp;title=JQuery+posts" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://codestips.com/jquery-posts/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=JQuery+posts&amp;u=http://codestips.com/jquery-posts/" rel="nofollow" title="Add to&nbsp;FURL"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/furl.png" title="Add to&nbsp;FURL" alt="Add to&nbsp;FURL" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://codestips.com/jquery-posts/&amp;title=JQuery+posts" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.kaboodle.com/za/selectpage?p_pop=false&amp;pa=url&amp;u=http://codestips.com/jquery-posts/" rel="nofollow" title="Add to&nbsp;Kaboodle"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/kaboodle.png" title="Add to&nbsp;Kaboodle" alt="Add to&nbsp;Kaboodle" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http://codestips.com/jquery-posts/&amp;T=JQuery+posts" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://netvouz.com/action/submitBookmark?url=http://codestips.com/jquery-posts/&amp;title=JQuery+posts&amp;popup=no" rel="nofollow" title="Add to&nbsp;Netvouz"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netvouz.png" title="Add to&nbsp;Netvouz" alt="Add to&nbsp;Netvouz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://codestips.com/jquery-posts/&amp;title=JQuery+posts" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.simpy.com/simpy/LinkAdd.do?href=http://codestips.com/jquery-posts/&amp;title=JQuery+posts" rel="nofollow" title="Add to&nbsp;Simpy"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/simpy.png" title="Add to&nbsp;Simpy" alt="Add to&nbsp;Simpy" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://codestips.com/jquery-posts/&amp;title=JQuery+posts" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.squidoo.com/lensmaster/bookmark?http://codestips.com/jquery-posts/" rel="nofollow" title="Add to&nbsp;Squidoo"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/squidoo.png" title="Add to&nbsp;Squidoo" alt="Add to&nbsp;Squidoo" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.spurl.net/spurl.php?url=http://codestips.com/jquery-posts/&amp;title=JQuery+posts" rel="nofollow" title="Add to&nbsp;Spurl"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/spurl.png" title="Add to&nbsp;Spurl" alt="Add to&nbsp;Spurl" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://codestips.com/jquery-posts/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://codestips.com/jquery-posts/&amp;t=JQuery+posts" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://codestips.com/jquery-posts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP rss feed parser class</title>
		<link>http://codestips.com/php-rss-feed-parser-class/</link>
		<comments>http://codestips.com/php-rss-feed-parser-class/#comments</comments>
		<pubDate>Tue, 10 May 2011 19:49:38 +0000</pubDate>
		<dc:creator>Codes Tips</dc:creator>
				<category><![CDATA[Codes]]></category>
		<category><![CDATA[PHP/MYSQL]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://codestips.com/?p=342</guid>
		<description><![CDATA[If you thought how to build rss feed parser you should think first think about what php xml parse you should use. If we examine the parser that we have built in then you will find expat, simple xml and xml dom manupulation. In order to parse a feed from a url you should first [...]]]></description>
			<content:encoded><![CDATA[<span class="fb_share"><fb:like href="http://codestips.com/php-rss-feed-parser-class/" layout="box_count"></fb:like></span><fb:like href='http%3A%2F%2Fcodestips.com%2Fphp-rss-feed-parser-class%2F' send='true' layout='button_count' show_faces='true' width='450' height='65' action='recommend' colorscheme='light' font='lucida+grande'></fb:like><p>If you thought how to build rss feed parser you should think first think about what php xml parse you should use. If we examine the parser that we have built in then you will find expat, simple xml and xml dom manupulation. In order to parse a feed from a url you should first validate the feed url , get the contents of the xml through file_get_contents, and loop through nodes of xml and get the nodes values.<br />
So here are steps the in order to build our class:<br />
1. Validate url<br />
2. Get contents using file_get_contents</p>
<p>3.Set the limit of how many items to return<br />
4. Parse results. We should replace the spaces of the node values.</p>
<p><strong><b>Class Description</b></strong><br />
<strong>Variables</strong><br />
<em>$url</em> &#8211; the url of the feed that will be parsed.<br />
<em>$doc</em> &#8211; the DOM Document that will load the xml parsed from url.<br />
<em>$limit</em> &#8211; Limits the number of items to be retrieved.<br />
<em>$isValidUrl</em> &#8211; Verifies if the link is valid.<br />
<em>$items</em> &#8211; the RSS items that would be returned.</p>
<p><strong>Constructor</strong><br />
Initialize the url of the feed and all class variables with empty variables. We initialize the limit to 5 items.<br />
<strong>Methods</strong><br />
<em>SetLimit</em> &#8211; sets the limit of the items to be parsed<br />
<em>ValidateUrl</em> &#8211; validates the url of the rss feed. It checks if the feed url begins with &#8216;http://&#8217;<br />
<em>ExecuteRssParser</em> &#8211; fetches the feed url and loads into a php variable. After that it loads the xml and parses the rss nodes values and returns the results into the variable &#8216;feeds&#8217;.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000000; font-weight: bold;">class</span> RssFeedParser
<span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$doc</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$isValidUrl</span><span style="color: #339933;">,</span> <span style="color: #000088;">$items</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$url</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">items</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doc</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">limit</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> ValidateUrl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">stristr</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'http://'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #000000; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SetLimit<span style="color: #009900;">&#40;</span><span style="color: #000088;">$limit</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">limit</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$limit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ExecuteRssParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValidUrl</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ValidateUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValidUrl</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$xmlText</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&gt;\s+&lt;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&gt;&lt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadXML</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xmlText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'item'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
	  <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'item'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$itemRSS</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span> 
		  <span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">,</span>
		  <span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">,</span>
		  <span style="color: #0000ff;">'link'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">,</span>
		  <span style="color: #0000ff;">'pubDate'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pubDate'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span>
		  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">items</span><span style="color: #339933;">,</span> <span style="color: #000088;">$itemRSS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$count</span><span style="color: #339933;">++;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #339933;">&gt;=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">limit</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	  <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>You can easy modificate this class in order to fit your needs. Good luck!!!</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://blinklist.com/index.php?Action=Blink/addblink.php&amp;Name=PHP+rss+feed+parser+class&amp;Description=PHP+rss+feed+parser+class&amp;Url=http://codestips.com/php-rss-feed-parser-class/" rel="nofollow" title="Add to&nbsp;BlinkList"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/blinklist.png" title="Add to&nbsp;BlinkList" alt="Add to&nbsp;BlinkList" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://codestips.com/php-rss-feed-parser-class/&amp;title=PHP+rss+feed+parser+class" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://codestips.com/php-rss-feed-parser-class/&amp;title=PHP+rss+feed+parser+class" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=PHP+rss+feed+parser+class&amp;url=http://codestips.com/php-rss-feed-parser-class/&amp;title=PHP+rss+feed+parser+class" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://codestips.com/php-rss-feed-parser-class/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=PHP+rss+feed+parser+class&amp;u=http://codestips.com/php-rss-feed-parser-class/" rel="nofollow" title="Add to&nbsp;FURL"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/furl.png" title="Add to&nbsp;FURL" alt="Add to&nbsp;FURL" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://codestips.com/php-rss-feed-parser-class/&amp;title=PHP+rss+feed+parser+class" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.kaboodle.com/za/selectpage?p_pop=false&amp;pa=url&amp;u=http://codestips.com/php-rss-feed-parser-class/" rel="nofollow" title="Add to&nbsp;Kaboodle"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/kaboodle.png" title="Add to&nbsp;Kaboodle" alt="Add to&nbsp;Kaboodle" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http://codestips.com/php-rss-feed-parser-class/&amp;T=PHP+rss+feed+parser+class" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://netvouz.com/action/submitBookmark?url=http://codestips.com/php-rss-feed-parser-class/&amp;title=PHP+rss+feed+parser+class&amp;popup=no" rel="nofollow" title="Add to&nbsp;Netvouz"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netvouz.png" title="Add to&nbsp;Netvouz" alt="Add to&nbsp;Netvouz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://codestips.com/php-rss-feed-parser-class/&amp;title=PHP+rss+feed+parser+class" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.simpy.com/simpy/LinkAdd.do?href=http://codestips.com/php-rss-feed-parser-class/&amp;title=PHP+rss+feed+parser+class" rel="nofollow" title="Add to&nbsp;Simpy"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/simpy.png" title="Add to&nbsp;Simpy" alt="Add to&nbsp;Simpy" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://codestips.com/php-rss-feed-parser-class/&amp;title=PHP+rss+feed+parser+class" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.squidoo.com/lensmaster/bookmark?http://codestips.com/php-rss-feed-parser-class/" rel="nofollow" title="Add to&nbsp;Squidoo"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/squidoo.png" title="Add to&nbsp;Squidoo" alt="Add to&nbsp;Squidoo" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.spurl.net/spurl.php?url=http://codestips.com/php-rss-feed-parser-class/&amp;title=PHP+rss+feed+parser+class" rel="nofollow" title="Add to&nbsp;Spurl"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/spurl.png" title="Add to&nbsp;Spurl" alt="Add to&nbsp;Spurl" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://codestips.com/php-rss-feed-parser-class/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://codestips.com/php-rss-feed-parser-class/&amp;t=PHP+rss+feed+parser+class" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://codestips.com/php-rss-feed-parser-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP parse youtube video id</title>
		<link>http://codestips.com/php-parse-youtube-video-id/</link>
		<comments>http://codestips.com/php-parse-youtube-video-id/#comments</comments>
		<pubDate>Tue, 10 May 2011 18:53:05 +0000</pubDate>
		<dc:creator>Codes Tips</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://codestips.com/?p=337</guid>
		<description><![CDATA[In order to parse the youtube video id you can use the parse_url and parse_str php functions. You should first parse the url and all it&#8217;s components using the php function parse_url. The components we are interested in for parsing the youtube video id you should use the &#8216;query&#8217; components where are stored all the [...]]]></description>
			<content:encoded><![CDATA[<span class="fb_share"><fb:like href="http://codestips.com/php-parse-youtube-video-id/" layout="box_count"></fb:like></span><fb:like href='http%3A%2F%2Fcodestips.com%2Fphp-parse-youtube-video-id%2F' send='true' layout='button_count' show_faces='true' width='450' height='65' action='recommend' colorscheme='light' font='lucida+grande'></fb:like><p>In order to parse the youtube video id you can use the parse_url and parse_str php functions. You should first parse the url and all it&#8217;s components using the php function parse_url. The components we are interested in for parsing the youtube video id you should use the &#8216;query&#8217; components where are stored all the query strings parameters. We then have to use the parse_str function in order to parse the query string in variables.</p>
<p>The input parameter of the function is the youtube url.<br />
The ouput paramter is the youtube video id.<br />
The php function that returns the youtube video id is:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> GetYouTubeVideoId<span style="color: #009900;">&#40;</span><span style="color: #000088;">$youtubeUrl</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #990000;">parse_url</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$youtubeUrl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">parse_str</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$qs</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$youtubeId</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$qs</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'v'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$youtubeId</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://blinklist.com/index.php?Action=Blink/addblink.php&amp;Name=PHP+parse+youtube+video+id&amp;Description=PHP+parse+youtube+video+id&amp;Url=http://codestips.com/php-parse-youtube-video-id/" rel="nofollow" title="Add to&nbsp;BlinkList"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/blinklist.png" title="Add to&nbsp;BlinkList" alt="Add to&nbsp;BlinkList" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://codestips.com/php-parse-youtube-video-id/&amp;title=PHP+parse+youtube+video+id" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://codestips.com/php-parse-youtube-video-id/&amp;title=PHP+parse+youtube+video+id" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=PHP+parse+youtube+video+id&amp;url=http://codestips.com/php-parse-youtube-video-id/&amp;title=PHP+parse+youtube+video+id" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://codestips.com/php-parse-youtube-video-id/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=PHP+parse+youtube+video+id&amp;u=http://codestips.com/php-parse-youtube-video-id/" rel="nofollow" title="Add to&nbsp;FURL"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/furl.png" title="Add to&nbsp;FURL" alt="Add to&nbsp;FURL" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://codestips.com/php-parse-youtube-video-id/&amp;title=PHP+parse+youtube+video+id" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.kaboodle.com/za/selectpage?p_pop=false&amp;pa=url&amp;u=http://codestips.com/php-parse-youtube-video-id/" rel="nofollow" title="Add to&nbsp;Kaboodle"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/kaboodle.png" title="Add to&nbsp;Kaboodle" alt="Add to&nbsp;Kaboodle" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http://codestips.com/php-parse-youtube-video-id/&amp;T=PHP+parse+youtube+video+id" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://netvouz.com/action/submitBookmark?url=http://codestips.com/php-parse-youtube-video-id/&amp;title=PHP+parse+youtube+video+id&amp;popup=no" rel="nofollow" title="Add to&nbsp;Netvouz"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netvouz.png" title="Add to&nbsp;Netvouz" alt="Add to&nbsp;Netvouz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://codestips.com/php-parse-youtube-video-id/&amp;title=PHP+parse+youtube+video+id" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.simpy.com/simpy/LinkAdd.do?href=http://codestips.com/php-parse-youtube-video-id/&amp;title=PHP+parse+youtube+video+id" rel="nofollow" title="Add to&nbsp;Simpy"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/simpy.png" title="Add to&nbsp;Simpy" alt="Add to&nbsp;Simpy" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://codestips.com/php-parse-youtube-video-id/&amp;title=PHP+parse+youtube+video+id" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.squidoo.com/lensmaster/bookmark?http://codestips.com/php-parse-youtube-video-id/" rel="nofollow" title="Add to&nbsp;Squidoo"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/squidoo.png" title="Add to&nbsp;Squidoo" alt="Add to&nbsp;Squidoo" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.spurl.net/spurl.php?url=http://codestips.com/php-parse-youtube-video-id/&amp;title=PHP+parse+youtube+video+id" rel="nofollow" title="Add to&nbsp;Spurl"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/spurl.png" title="Add to&nbsp;Spurl" alt="Add to&nbsp;Spurl" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://codestips.com/php-parse-youtube-video-id/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://codestips.com/php-parse-youtube-video-id/&amp;t=PHP+parse+youtube+video+id" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://codestips.com/php-parse-youtube-video-id/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP downloadable file</title>
		<link>http://codestips.com/php-downloadable-file/</link>
		<comments>http://codestips.com/php-downloadable-file/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 21:40:13 +0000</pubDate>
		<dc:creator>Codes Tips</dc:creator>
				<category><![CDATA[PHP/MYSQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://codestips.com/?p=319</guid>
		<description><![CDATA[Text downloadable file Last days I needed to create a file which had to be downloaded by many users. The file had to be in plain text and contents from file come from database. Because I didn&#8217;t had a separate folder for each user I couldn&#8217;t write the information to a file to the server [...]]]></description>
			<content:encoded><![CDATA[<span class="fb_share"><fb:like href="http://codestips.com/php-downloadable-file/" layout="box_count"></fb:like></span><fb:like href='http%3A%2F%2Fcodestips.com%2Fphp-downloadable-file%2F' send='true' layout='button_count' show_faces='true' width='450' height='65' action='recommend' colorscheme='light' font='lucida+grande'></fb:like><h2>Text downloadable file</h2>
<p>Last days I needed to create a file which had to be downloaded by many users. The file had to be in plain text and contents from file come from database. Because I didn&#8217;t had a separate folder for each user I couldn&#8217;t write the information to a file to the server and put a link in order the user to download it. I thought a little bit how to handle it and found a very simple solution to to make a file downloadable to users. I even try to other popular formats like .doc, .xls,.csv,.dat and it works really nice. Extension that I tested it and  didn&#8217;t worked are .xlsx and .pdf.</p>
<p>In order to make a file downloadable you should modify the header of the page to make <strong>Content-type: application/octet-stream</strong> and <strong>Content-Disposition: attachment; filename=\&#8221;downloadableFile.extension\&#8221; </strong> in order  to inform the browser that there is an attachment binary file. The file will open in a window to inform the user it to open it with a default application or save it to disk.<br />
Code for downloadable file:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: application/octet-stream&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Disposition: attachment; filename=<span style="color: #000099; font-weight: bold;">\&quot;</span>downloadablefile.txt<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">echo</span> <span style="color: #0000ff;">'Contents of plain text downloadable file'</span><span style="color: #339933;">;</span>  
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h2>CSV downloadable file</h2>
<p>The real magic comes when you wanna make csv file downloadable. You can read data from a mysql table and write the contents to a csv file. You should only read contents from a table and using echo output to the file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: application/octet-stream&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Disposition: attachment; filename=<span style="color: #000099; font-weight: bold;">\&quot;</span>downloadablefile.csv<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$connect</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$server</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span>
			or <span style="color: #990000;">die</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Database CONNECT Error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
 		<span style="color: #000088;">$resultcsv</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_db_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;select * from <span style="color: #006699; font-weight: bold;">$csvTable</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resultcsv</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span> 
			<span style="color: #990000;">echo</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Name&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;City&quot;</span><span style="color: #009900;">&#93;</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: #009900;">&#125;</span>
		<span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connect</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h2>Excel downloadable file</h2>
<p>Code for generating a excel downloadble file:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: application/octet-stream&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Disposition: attachment; filename=<span style="color: #000099; font-weight: bold;">\&quot;</span>downloadableFile.xls<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$connect</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$server</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span>
			or <span style="color: #990000;">die</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Database CONNECT Error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
 		<span style="color: #000088;">$resultxls</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_db_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;select * from <span style="color: #006699; font-weight: bold;">$xlsTable</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resultxls</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span> 
			<span style="color: #990000;">echo</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Name&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;City&quot;</span><span style="color: #009900;">&#93;</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: #009900;">&#125;</span>
		<span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connect</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>You can look through records from database and output as I did for csv</p>
<h2>Doc downloadable file</h2>
<p>You can even make a downloadable .doc files by changing the file extension to doc.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: application/octet-stream&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Disposition: attachment; filename=<span style="color: #000099; font-weight: bold;">\&quot;</span>downloadablefile.doc<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">echo</span> <span style="color: #0000ff;">'Contents of Word Document.'</span><span style="color: #339933;">;</span>  
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://blinklist.com/index.php?Action=Blink/addblink.php&amp;Name=PHP+downloadable+file&amp;Description=PHP+downloadable+file&amp;Url=http://codestips.com/php-downloadable-file/" rel="nofollow" title="Add to&nbsp;BlinkList"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/blinklist.png" title="Add to&nbsp;BlinkList" alt="Add to&nbsp;BlinkList" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://codestips.com/php-downloadable-file/&amp;title=PHP+downloadable+file" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://codestips.com/php-downloadable-file/&amp;title=PHP+downloadable+file" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=PHP+downloadable+file&amp;url=http://codestips.com/php-downloadable-file/&amp;title=PHP+downloadable+file" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://codestips.com/php-downloadable-file/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=PHP+downloadable+file&amp;u=http://codestips.com/php-downloadable-file/" rel="nofollow" title="Add to&nbsp;FURL"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/furl.png" title="Add to&nbsp;FURL" alt="Add to&nbsp;FURL" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://codestips.com/php-downloadable-file/&amp;title=PHP+downloadable+file" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.kaboodle.com/za/selectpage?p_pop=false&amp;pa=url&amp;u=http://codestips.com/php-downloadable-file/" rel="nofollow" title="Add to&nbsp;Kaboodle"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/kaboodle.png" title="Add to&nbsp;Kaboodle" alt="Add to&nbsp;Kaboodle" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http://codestips.com/php-downloadable-file/&amp;T=PHP+downloadable+file" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://netvouz.com/action/submitBookmark?url=http://codestips.com/php-downloadable-file/&amp;title=PHP+downloadable+file&amp;popup=no" rel="nofollow" title="Add to&nbsp;Netvouz"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netvouz.png" title="Add to&nbsp;Netvouz" alt="Add to&nbsp;Netvouz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://codestips.com/php-downloadable-file/&amp;title=PHP+downloadable+file" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.simpy.com/simpy/LinkAdd.do?href=http://codestips.com/php-downloadable-file/&amp;title=PHP+downloadable+file" rel="nofollow" title="Add to&nbsp;Simpy"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/simpy.png" title="Add to&nbsp;Simpy" alt="Add to&nbsp;Simpy" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://codestips.com/php-downloadable-file/&amp;title=PHP+downloadable+file" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.squidoo.com/lensmaster/bookmark?http://codestips.com/php-downloadable-file/" rel="nofollow" title="Add to&nbsp;Squidoo"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/squidoo.png" title="Add to&nbsp;Squidoo" alt="Add to&nbsp;Squidoo" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.spurl.net/spurl.php?url=http://codestips.com/php-downloadable-file/&amp;title=PHP+downloadable+file" rel="nofollow" title="Add to&nbsp;Spurl"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/spurl.png" title="Add to&nbsp;Spurl" alt="Add to&nbsp;Spurl" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://codestips.com/php-downloadable-file/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://codestips.com/php-downloadable-file/&amp;t=PHP+downloadable+file" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://codestips.com/php-downloadable-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP string manipulation 3</title>
		<link>http://codestips.com/php-string-manipulation-3/</link>
		<comments>http://codestips.com/php-string-manipulation-3/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 21:00:01 +0000</pubDate>
		<dc:creator>Codes Tips</dc:creator>
				<category><![CDATA[Codes]]></category>
		<category><![CDATA[PHP/MYSQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://codestips.com/?p=298</guid>
		<description><![CDATA[This is the third part about how to manipulate strings in PHP. Replace all occurrences of sub strings with a replacement string If you wanna replace all occurrence of a substring inside a string you should use the function str_replace passing as first argument the string(or array of strings) you wanna search, second argument the [...]]]></description>
			<content:encoded><![CDATA[<span class="fb_share"><fb:like href="http://codestips.com/php-string-manipulation-3/" layout="box_count"></fb:like></span><fb:like href='http%3A%2F%2Fcodestips.com%2Fphp-string-manipulation-3%2F' send='true' layout='button_count' show_faces='true' width='450' height='65' action='recommend' colorscheme='light' font='lucida+grande'></fb:like><p>This is the <strong>third part</strong> about how to manipulate strings in PHP.</p>
<h2>Replace all occurrences of sub strings with a replacement string</h2>
<p>If you wanna replace all occurrence of a substring inside a string you should use the function <b>str_replace</b> passing as first argument the string(or array of strings) you wanna search, second argument the string(or array of strings) you wanna make the replace, the third is the string you wanna search in and the fourth argument is the number of replacements the function should make. It returns a string after all replacements have been made.<br />
<b>Declaration</b>: <i>mixed <b>str_replace</b> ( mixed $SearchFor, mixed $ReplaceWith , mixed $String[, int &#038;$number] )</i><br />
<b>Example</b>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$string</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is a str_replace Function Test&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$searchFor</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Function&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replaceWith</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;function&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$searchFor</span><span style="color: #339933;">,</span><span style="color: #000088;">$replaceWith</span><span style="color: #339933;">,</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><b>Output</b>: This is a str_replace function test</p>
<h2>Split string one by one</h2>
<p>To tokenize string in php you can you the function <b>strtok</b> in order to split a string. Each smaller string it&#8217;s separated by a delimeter. It takes as a first argument the string that will be split and the second the delimeter that it is used to split it.<br />
You should call the first time the function as strtok($String,$delimiter) and returns the first string that is delimited. Any subsequent calls of the function should be like strto($delimiter(s)), because the function keeps into memory the variable $String that it&#8217;s delimited.<br />
You can specify multiple delimiter<br />
<b>Declaration</b>: string <b>strtok</b>(string $String, string $delimiter(s))<br />
<b>Example</b>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is an strtok Function Test&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tok</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtok</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tok</span> <span style="color: #339933;">!==</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;String=<span style="color: #006699; font-weight: bold;">$tok</span>&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$tok</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtok</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//returns the next splitted string in the $string variable</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><b>Output</b>:<br />
String=This<br />
String=is<br />
String=an<br />
String=strtok<br />
String=Function<br />
String=Test</p>
<h2>Split a string by a delimeter</h2>
<p>Disadvantage of strtok function it&#8217;s that you should call it every time you wanna split the string. You can use <b>explode</b> function in order to split the string and returns an array of string with all the string  splitted. The first argument is the delimiter, the second the string you wanna split and you can also specify an optional integer argument that specify the limit of strings splitted.<br />
<b>Declaration</b>: array <b>xplode</b>(string $delimiter , string $String[, int $limit ])<br />
<b>Example</b>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is an explode Function Test&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$explodedstring</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$explodedstring</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$substring</span><span style="color: #009900;">&#41;</span>
 <span style="color: #990000;">echo</span> <span style="color: #000088;">$substring</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><b>Output</b>:<br />
This<br />
is<br />
an explode Function Test</p>
<h2>Union elements of array in a string</h2>
<p>If you wanna join array elements into 1 string you should call the function <b>implode</b>. The first argument that you should pass to the function is the separation and the second one the array of string.<br />
<b>Declaration</b>: string implode ( string $separator , array $Strings)<br />
<b>Example</b>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$strings</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'This'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'is'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'implode'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'function'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$separator</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$separator</span><span style="color: #339933;">,</span><span style="color: #000088;">$strings</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><b>Output</b>:<br />
This is a implode function test</p>
<h2>Replace a string by regular expression</h2>
<p>You can use regular expression in php to replace using the function <b>preg_replace</b>. You should pass the first argument as a regular expression pattern &#8211; it can be a string or an array of string, the second is the string or array of string that you use to replace the pattern, third argument is the string you search in. The final two arguments are optional, first parameter is the limit of replacement you should make and the final one is the value of replacements that have been made in the string.<br />
<b>Declaration</b>: mixed <b>preg_replace</b> (mixed $regularExpression, mixed $replaceWith, mixed $String [, int $limit= -1 [, int &#038;$count ]] )<br />
<b>Example</b>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is a preg_replace bad, words, Function Test&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;(\w+,)&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replaceWith</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #000088;">$replaceWith</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</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;">?&gt;</span></pre></div></div>

<p><b>Output</b>:<br />
This is a preg_replace Function Test</p>
<p><a href="http://www.codestips.com/php-string-manipulation-2/" style="color:red">PHP string manipulation &#8211; part 2</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://blinklist.com/index.php?Action=Blink/addblink.php&amp;Name=PHP+string+manipulation+3&amp;Description=PHP+string+manipulation+3&amp;Url=http://codestips.com/php-string-manipulation-3/" rel="nofollow" title="Add to&nbsp;BlinkList"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/blinklist.png" title="Add to&nbsp;BlinkList" alt="Add to&nbsp;BlinkList" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://codestips.com/php-string-manipulation-3/&amp;title=PHP+string+manipulation+3" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://codestips.com/php-string-manipulation-3/&amp;title=PHP+string+manipulation+3" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=PHP+string+manipulation+3&amp;url=http://codestips.com/php-string-manipulation-3/&amp;title=PHP+string+manipulation+3" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://codestips.com/php-string-manipulation-3/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=PHP+string+manipulation+3&amp;u=http://codestips.com/php-string-manipulation-3/" rel="nofollow" title="Add to&nbsp;FURL"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/furl.png" title="Add to&nbsp;FURL" alt="Add to&nbsp;FURL" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://codestips.com/php-string-manipulation-3/&amp;title=PHP+string+manipulation+3" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.kaboodle.com/za/selectpage?p_pop=false&amp;pa=url&amp;u=http://codestips.com/php-string-manipulation-3/" rel="nofollow" title="Add to&nbsp;Kaboodle"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/kaboodle.png" title="Add to&nbsp;Kaboodle" alt="Add to&nbsp;Kaboodle" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http://codestips.com/php-string-manipulation-3/&amp;T=PHP+string+manipulation+3" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://netvouz.com/action/submitBookmark?url=http://codestips.com/php-string-manipulation-3/&amp;title=PHP+string+manipulation+3&amp;popup=no" rel="nofollow" title="Add to&nbsp;Netvouz"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netvouz.png" title="Add to&nbsp;Netvouz" alt="Add to&nbsp;Netvouz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://codestips.com/php-string-manipulation-3/&amp;title=PHP+string+manipulation+3" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.simpy.com/simpy/LinkAdd.do?href=http://codestips.com/php-string-manipulation-3/&amp;title=PHP+string+manipulation+3" rel="nofollow" title="Add to&nbsp;Simpy"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/simpy.png" title="Add to&nbsp;Simpy" alt="Add to&nbsp;Simpy" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://codestips.com/php-string-manipulation-3/&amp;title=PHP+string+manipulation+3" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.squidoo.com/lensmaster/bookmark?http://codestips.com/php-string-manipulation-3/" rel="nofollow" title="Add to&nbsp;Squidoo"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/squidoo.png" title="Add to&nbsp;Squidoo" alt="Add to&nbsp;Squidoo" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.spurl.net/spurl.php?url=http://codestips.com/php-string-manipulation-3/&amp;title=PHP+string+manipulation+3" rel="nofollow" title="Add to&nbsp;Spurl"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/spurl.png" title="Add to&nbsp;Spurl" alt="Add to&nbsp;Spurl" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://codestips.com/php-string-manipulation-3/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://codestips.com/php-string-manipulation-3/&amp;t=PHP+string+manipulation+3" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://codestips.com/php-string-manipulation-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP string manipulation 2</title>
		<link>http://codestips.com/php-string-manipulation-2/</link>
		<comments>http://codestips.com/php-string-manipulation-2/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 22:34:25 +0000</pubDate>
		<dc:creator>Codes Tips</dc:creator>
				<category><![CDATA[Codes]]></category>
		<category><![CDATA[PHP/MYSQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://codestips.com/?p=274</guid>
		<description><![CDATA[This is the second part of the how you can manipulate string in PHP. Find length of a string If you want to find out how many characters have a string you can use the function strlen. If you have to pass 1 argument as the string you wish you find out the length and [...]]]></description>
			<content:encoded><![CDATA[<span class="fb_share"><fb:like href="http://codestips.com/php-string-manipulation-2/" layout="box_count"></fb:like></span><fb:like href='http%3A%2F%2Fcodestips.com%2Fphp-string-manipulation-2%2F' send='true' layout='button_count' show_faces='true' width='450' height='65' action='recommend' colorscheme='light' font='lucida+grande'></fb:like><p>This is the <strong>second part</strong> of the how you can manipulate string in PHP.</p>
<h2>Find length of a string</h2>
<p>If you want to find out how many characters have a string you can use the function <strong>strlen</strong>. If you have to pass 1 argument as the string you wish you find out the length and it returns an integer.<br />
<strong>Declaration</strong>: <em>int <strong>strlen</strong>(string text)</em><br />
<strong>Example</strong>:</p>
<p><strong>Output</strong>: 31</p>
<h2>Find a sub string inside a string</h2>
<p>To find a first occurrence of a string inside other you should use the function <strong>strstr</strong> and pass it 2 arguments. First argument should be the string in which you wanna look and the second should be the string you search. It returns part of the first occurrence until the end of the string you search in.<br />
<strong>Declaration</strong>: <em>string <strong>strstr</strong>(string searchIn,string lookfor)</em><br />
<strong>Example</strong>:</p>
<p><strong>Output</strong>: strstr Function Test</p>
<h2>Find a string inside other string(case insensitive)</h2>
<p>To find an occurrence of a string inside another with case insensitive activated you should use the function <strong>stristr</strong>. It has the same parameters as the <strong>strstr</strong>, but it also finds case insensitive strings.<br />
<strong>Declaration</strong>: <em>string <strong>stristr</strong>(string SearchIn, string lookFor)</em><br />
<strong>Example</strong>:</p>
<p><strong>Output</strong>: StrStr Function Test</p>
<h2>Find position of first occurrence of a string inside another</h2>
<p>To find the position of the first occurrence of a string you can use the function <strong>strpos</strong>.  You should pass two arguments. The first parameter is the string you wanna search in and the second is a the string you are looking for. You can specify an additional argument, an offset that indicates from where position the search should begin.<br />
<strong>Declaration</strong>: <em>int <strong>strpos</strong> ( string $SearchIn, mixed $SearchFor[, int $offset= 0 ] )</em><br />
<strong>Example</strong>:</p>
<p><strong>Output</strong>: 11</p>
<h2>Find a position of a string inside another(case insensitive)</h2>
<p>You can use the function <strong>strripos</strong> and it has the same arguments as <strong>strpos</strong>, but it searches for case insensitives.<br />
<strong>Declaration</strong>:  <em>int <strong>strripos</strong> ( string $SearchIn, mixed $SearchFor[, int $offset= 0 ] )</em></p>
<h2>How to return parts of a string</h2>
<p>To return parts of a string you should use the <strong>substr</strong> function and pass it 2 arguments and 1 optional. The first argument is the string you wanna return a part of a string, second is the start position from where you wanna begin crop the string. You can also specify an argument with the length that the function should crop.<br />
<strong>Declaration</strong>: string <strong>substr</strong> ( string $SearchIn , int $from [,int $length])<br />
<strong>Example</strong>:</p>
<p><strong>Output</strong>: is a substr</p>
<h2>Insert after each end of line in string a html <em>br</em> tag</h2>
<p>To insert html tag br after the new lines in a string you can use the function <strong>nl2br</strong>. Function takes one neccessary  argument and one optional. First is a string where it will be inserted the br tag and the second one is a boolean that indicates if the replacement is a XHTML compatible line breaks(default value is TRUE).<br />
<strong>Declaration</strong>: string <strong>nl2br</strong>(string $InsertIn [,bool $isXHTML= true])<br />
<strong>Example</strong>:</p>
<p><strong>Output</strong>:<br />
This is a nl2br&lt;br /&gt;   Function&lt;br /&gt;   Test</p>
<p><a href="http://codestips.com/php-string-manipulation/" style="color:red">PHP string manipulation &#8211; part 1</a> &#8211; <a href="http://codestips.com/php-string-manipulation-3/" style="color:red">PHP string manipulation &#8211; part 3</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://blinklist.com/index.php?Action=Blink/addblink.php&amp;Name=PHP+string+manipulation+2&amp;Description=PHP+string+manipulation+2&amp;Url=http://codestips.com/php-string-manipulation-2/" rel="nofollow" title="Add to&nbsp;BlinkList"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/blinklist.png" title="Add to&nbsp;BlinkList" alt="Add to&nbsp;BlinkList" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://codestips.com/php-string-manipulation-2/&amp;title=PHP+string+manipulation+2" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://codestips.com/php-string-manipulation-2/&amp;title=PHP+string+manipulation+2" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=PHP+string+manipulation+2&amp;url=http://codestips.com/php-string-manipulation-2/&amp;title=PHP+string+manipulation+2" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://codestips.com/php-string-manipulation-2/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=PHP+string+manipulation+2&amp;u=http://codestips.com/php-string-manipulation-2/" rel="nofollow" title="Add to&nbsp;FURL"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/furl.png" title="Add to&nbsp;FURL" alt="Add to&nbsp;FURL" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://codestips.com/php-string-manipulation-2/&amp;title=PHP+string+manipulation+2" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.kaboodle.com/za/selectpage?p_pop=false&amp;pa=url&amp;u=http://codestips.com/php-string-manipulation-2/" rel="nofollow" title="Add to&nbsp;Kaboodle"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/kaboodle.png" title="Add to&nbsp;Kaboodle" alt="Add to&nbsp;Kaboodle" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http://codestips.com/php-string-manipulation-2/&amp;T=PHP+string+manipulation+2" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://netvouz.com/action/submitBookmark?url=http://codestips.com/php-string-manipulation-2/&amp;title=PHP+string+manipulation+2&amp;popup=no" rel="nofollow" title="Add to&nbsp;Netvouz"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netvouz.png" title="Add to&nbsp;Netvouz" alt="Add to&nbsp;Netvouz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://codestips.com/php-string-manipulation-2/&amp;title=PHP+string+manipulation+2" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.simpy.com/simpy/LinkAdd.do?href=http://codestips.com/php-string-manipulation-2/&amp;title=PHP+string+manipulation+2" rel="nofollow" title="Add to&nbsp;Simpy"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/simpy.png" title="Add to&nbsp;Simpy" alt="Add to&nbsp;Simpy" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://codestips.com/php-string-manipulation-2/&amp;title=PHP+string+manipulation+2" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.squidoo.com/lensmaster/bookmark?http://codestips.com/php-string-manipulation-2/" rel="nofollow" title="Add to&nbsp;Squidoo"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/squidoo.png" title="Add to&nbsp;Squidoo" alt="Add to&nbsp;Squidoo" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.spurl.net/spurl.php?url=http://codestips.com/php-string-manipulation-2/&amp;title=PHP+string+manipulation+2" rel="nofollow" title="Add to&nbsp;Spurl"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/spurl.png" title="Add to&nbsp;Spurl" alt="Add to&nbsp;Spurl" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://codestips.com/php-string-manipulation-2/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://codestips.com/php-string-manipulation-2/&amp;t=PHP+string+manipulation+2" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://codestips.com/php-string-manipulation-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP string manipulation</title>
		<link>http://codestips.com/php-string-manipulation/</link>
		<comments>http://codestips.com/php-string-manipulation/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 22:53:28 +0000</pubDate>
		<dc:creator>Codes Tips</dc:creator>
				<category><![CDATA[Codes]]></category>
		<category><![CDATA[PHP/MYSQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://codestips.com/?p=267</guid>
		<description><![CDATA[This is a first part of PHP string manipulation. It&#8217;s extremelly useful to know how to manipulate strings in PHP. This tutorial will guide you through how to use the functions that PHP offers. A string in PHP it&#8217;s defined in php in a variable assigning a single or double quote value like: $string = [...]]]></description>
			<content:encoded><![CDATA[<span class="fb_share"><fb:like href="http://codestips.com/php-string-manipulation/" layout="box_count"></fb:like></span><fb:like href='http%3A%2F%2Fcodestips.com%2Fphp-string-manipulation%2F' send='true' layout='button_count' show_faces='true' width='450' height='65' action='recommend' colorscheme='light' font='lucida+grande'></fb:like><p>This is a <strong>first part</strong> of PHP string manipulation.<br />
It&#8217;s extremelly useful to know how to manipulate strings in PHP. This tutorial will guide you through how to use the functions that PHP offers.<br />
A string in PHP it&#8217;s defined in php in a variable assigning a single or double quote  value like:<br />
$string = &#8216;This is a test string&#8217;;<br />
$string = &#8220;This is a test string&#8221;;</p>
<h2>Make all letters from string lowercase</h2>
<p>To make every letter of a string in lowercase you should use the function <strong>strtolower</strong> like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$string</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is a  strtolower Function Test&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The ouput will be: this is a strtolower function test</p>
<h2>Make all letters of a string uppercase</h2>
<p>You should use the function <strong>strtoupper</strong> in order to make every letter of the string in uppercase:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$string</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is a  strtoupper Function Test&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The ouput of will be: THIS IS A STRTOUPPER FUNCTION TEST</p>
<h2>Capitalize first letter of each word in a sentence with ucwords</h2>
<p>To make a sentence more estetic we can capitalize first letter of each word in a sentence with the function ucwords().</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$string</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is a  ucwords Function Test&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">ucwords</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span> 
Output: This Is A Ucwords Function Test</pre></div></div>

<p>This function touch only the first letter of each word and leave other characters like they are. We can improve the way how the string is displayed by combining with the strtolower function.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$string</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ThiS is a  ucwords FunctioN Test&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">ucwords</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Output will be:This Is A Ucwords Function Test</p>
<h2>Remove tabs,spaces,new lines from beginning and end of a string</h2>
<p>To remove spaces,tabs and new lines from the beginning and end of a string you should use the function trim(). This function accepts as an input a string, that will be cleaned and returns a string without spaces,tabs and new lines.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$string</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span> <span style="color: #000099; font-weight: bold;">\n</span>This is a  trim Function Test   &quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Output: This is a trim Function Test</p>
<h2>Remove spaces,tabs and new lines from begining of a string</h2>
<p>Similar like trim we can use ltrim to remove spaces,tabs and new lines from the beginning of a string</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$string</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span> <span style="color: #000099; font-weight: bold;">\n</span>This is a  trim Function Test <span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>  &quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">ltrim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Output: This is a  trim Function Test 		   </p>
<h2>Remove spaces,tabs and new lines from the end of a string</h2>
<p>Similar like ltrim we can use rtrim to remove characters that we don&#8217;t need:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$string</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span> <span style="color: #000099; font-weight: bold;">\n</span>This is a  rtrim Function Test <span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>  &quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">rtrim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h2>Remove tags from a string</h2>
<p>To remove tags from a string you should use strip_tags to have a clean string without tags</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$string</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;pre&gt;This is a  trim Function Test&lt;/ pre&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><a href="http://codestips.com/php-string-manipulation-2/" style="color:red">PHP string manipulation &#8211; part 2</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://blinklist.com/index.php?Action=Blink/addblink.php&amp;Name=PHP+string+manipulation&amp;Description=PHP+string+manipulation&amp;Url=http://codestips.com/php-string-manipulation/" rel="nofollow" title="Add to&nbsp;BlinkList"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/blinklist.png" title="Add to&nbsp;BlinkList" alt="Add to&nbsp;BlinkList" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://codestips.com/php-string-manipulation/&amp;title=PHP+string+manipulation" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://codestips.com/php-string-manipulation/&amp;title=PHP+string+manipulation" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=PHP+string+manipulation&amp;url=http://codestips.com/php-string-manipulation/&amp;title=PHP+string+manipulation" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://codestips.com/php-string-manipulation/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=PHP+string+manipulation&amp;u=http://codestips.com/php-string-manipulation/" rel="nofollow" title="Add to&nbsp;FURL"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/furl.png" title="Add to&nbsp;FURL" alt="Add to&nbsp;FURL" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://codestips.com/php-string-manipulation/&amp;title=PHP+string+manipulation" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.kaboodle.com/za/selectpage?p_pop=false&amp;pa=url&amp;u=http://codestips.com/php-string-manipulation/" rel="nofollow" title="Add to&nbsp;Kaboodle"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/kaboodle.png" title="Add to&nbsp;Kaboodle" alt="Add to&nbsp;Kaboodle" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http://codestips.com/php-string-manipulation/&amp;T=PHP+string+manipulation" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://netvouz.com/action/submitBookmark?url=http://codestips.com/php-string-manipulation/&amp;title=PHP+string+manipulation&amp;popup=no" rel="nofollow" title="Add to&nbsp;Netvouz"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/netvouz.png" title="Add to&nbsp;Netvouz" alt="Add to&nbsp;Netvouz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://codestips.com/php-string-manipulation/&amp;title=PHP+string+manipulation" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.simpy.com/simpy/LinkAdd.do?href=http://codestips.com/php-string-manipulation/&amp;title=PHP+string+manipulation" rel="nofollow" title="Add to&nbsp;Simpy"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/simpy.png" title="Add to&nbsp;Simpy" alt="Add to&nbsp;Simpy" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://codestips.com/php-string-manipulation/&amp;title=PHP+string+manipulation" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.squidoo.com/lensmaster/bookmark?http://codestips.com/php-string-manipulation/" rel="nofollow" title="Add to&nbsp;Squidoo"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/squidoo.png" title="Add to&nbsp;Squidoo" alt="Add to&nbsp;Squidoo" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.spurl.net/spurl.php?url=http://codestips.com/php-string-manipulation/&amp;title=PHP+string+manipulation" rel="nofollow" title="Add to&nbsp;Spurl"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/spurl.png" title="Add to&nbsp;Spurl" alt="Add to&nbsp;Spurl" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://codestips.com/php-string-manipulation/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://codestips.com/php-string-manipulation/&amp;t=PHP+string+manipulation" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://codestips.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://codestips.com/php-string-manipulation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

