<?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>Web Media Magazine &#187; PHP</title>
	<atom:link href="http://www.webmediamagazine.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webmediamagazine.com</link>
	<description>Web development magazine, including Photoshop tutorials, PHP tutorials, CSS tutorials, web news and trends.</description>
	<lastBuildDate>Sun, 20 Sep 2009 13:43:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Create Dynamic Images With PHP/MySQL</title>
		<link>http://www.webmediamagazine.com/php/create-dynamic-images-with-phpmysql/</link>
		<comments>http://www.webmediamagazine.com/php/create-dynamic-images-with-phpmysql/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 22:57:12 +0000</pubDate>
		<dc:creator>John Owen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Image]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.webmediamagazine.com/?p=1206</guid>
		<description><![CDATA[Learn how to generate dynamic images in PHP, some uses are tracking visitors with an image, displaying the users name in the image or the time/date. In this tutorial you will convert an email address from a database to an image (just like facebook) to protect from email spiders.]]></description>
			<content:encoded><![CDATA[<p>You can view the demo of what we will be creating here: <a href="http://www.webmediamagazine.com/demo/dynamic-images/">Dynamic Images</a><br />
I will be using my local server to do this tutorial, first off with need to create some data to use in this tutorial. I have provided a MYSQL Query string for you to use. I am using PHP 5.3 and MySQL 3.2.0.1.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">-- Table structure for table `people`
--
&nbsp;
CREATE TABLE IF NOT EXISTS `people` (
  `id` int(8) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(255) NOT NULL,
  `last_name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
&nbsp;
--
-- Dumping data for table `people`
--
&nbsp;
INSERT INTO `people` (`id`, `first_name`, `last_name`, `email`) VALUES
(1, 'Tim', 'Carpenter', 'tim@carpentercentral.com'),
(2, 'Joe', 'Woolen', 'joewoolen@gmail.com'),
(3, 'Harry', 'Melvis', 'hmelvis@live.com'),
(4, 'Jane', 'Suden', 'janes211@yahoo.com'),
(5, 'Sue', 'Baxter', 'suebax@hotmail.co.uk'),
(6, 'Jeremy', 'Yun', 'jerry@jerry123.com');</pre></div></div>

<p>
<h3>Summary</h3>
</p>
<p>You will be retrieving an email from the database then getting the length of the email and inserting it into an image thats size is custom to the length of the text. This prevents email spiders from reading the email.</p>
<p>
<h3>Step 1 &#8211; Connecting To The Database</h3>
</p>
<p>Create a new file called &#8220;config.php&#8221; and put this code in it:</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: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MYSQL_USER'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'root'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MYSQL_PASS'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MYSQL_DB'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data_db'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MYSQL_HOST'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$dbc</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">mysql_connect</span> <span style="color: #009900;">&#40;</span>MYSQL_HOST<span style="color: #339933;">,</span> MYSQL_USER<span style="color: #339933;">,</span> MYSQL_PASS<span style="color: #009900;">&#41;</span> OR <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Could not connect to MySQL: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">@</span><span style="color: #990000;">mysql_select_db</span> <span style="color: #009900;">&#40;</span>MYSQL_DB<span style="color: #009900;">&#41;</span> OR <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Could not select the database: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Enter your details at MYSQL_USER/PASS/DB, you wont need to change localhost unless you know you need to change it.</p>
<p>
<h3>Step 2 &#8211; Index.php</h3>
</p>
<p>Create a new file called index.php with this code:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;?php include('config.php'); ?&gt;
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;&gt;
&lt;title&gt;Dynamic Images&lt;/title&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>This is our template. In the index file we will list the names and then you will be able to click the name and view their email.</p>
<p>
<h3>Step 3 &#8211; Listing The Names</h3>
</p>
<p>Before we delve into the actual image creation we need to list our names. Insert this PHP code between your two body tags (<body>HERE</body>).</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: #666666; font-style: italic;">// Will select all fields except email from table 'poeple'. You can select all fields by using a * (eg. SELECT * FROM people)</span>
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id,first_name,last_name FROM people&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// Run the query &amp; grab the result , if error it will display the error</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Mysql error: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// We now have all data in the result, to retrieve this we need to use the mysql_fetch_array in a while loop</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;h3&gt;Names&lt;/h3&gt;
&lt;table width=&quot;300&quot; border=&quot;0&quot; cellspacing=&quot;5&quot; cellpadding=&quot;5&quot;&gt;
	&lt;tr&gt;
    	&lt;td width=&quot;32&quot;&gt;ID&lt;/td&gt;
    	&lt;td width=&quot;147&quot;&gt;Name&lt;/td&gt;
    	&lt;td width=&quot;71&quot;&gt;&lt;/td&gt;
  	&lt;/tr&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #666666; font-style: italic;">// the while loop will go though each row and display data from the coloumns</span>
	<span style="color: #666666; font-style: italic;">// the view email link will link to a page called view_email.php with a $_GET function</span>
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		&lt;tr&gt;
    		&lt;td width=&quot;32&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
    		&lt;td width=&quot;147&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'last_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// combines the first &amp; last name to display full name ?&gt;</span>&lt;/td&gt;
    		&lt;td width=&quot;71&quot;&gt;&lt;a href=&quot;view_email.php?id=<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// links the ID to the view_email.php page ?&gt;</span>&quot;&gt;View Email&lt;/a&gt;&lt;/td&gt;
  		&lt;/tr&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// in between the two PHP sections the code will go, this makes it easier to read?&gt;</span>
&lt;/table&gt;</pre></div></div>

<p><a href="http://www.webmediamagazine.com/wp-content/uploads/2009/08/names.jpg"><img src="http://www.webmediamagazine.com/wp-content/uploads/2009/08/names.jpg" alt="names" title="names" width="620" height="350" class="alignnone size-full wp-image-1218" /></a></p>
<p>
<h3>Step 4 &#8211; view_webcam.php</h3>
</p>
<p>Create a new file and name it &#8220;view_webcam.php&#8221;, enter this code in.</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: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;&gt;
&lt;title&gt;Dynamic Images&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #666666; font-style: italic;">//check if the $_GET variable 'id' isset, if it is run the image creation blabla</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// include the img.php file to create the email from the var id</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;img src=&quot;img.php?id='</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</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: #666666; font-style: italic;">// If no var has been set tell the visitor</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'No ID has been given.'</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Not much but it basically inserts an image with a link to a PHP script, the PHP script acts like a image by chaging the header content type to png.</p>
<p>
<h3>Step 5- img.php</h3>
</p>
<p>Create a new PHP file and name it img.php, this is the main part of out tutorial so i&#8217;ll explain a lot more <img src='http://www.webmediamagazine.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </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: #666666; font-style: italic;">// Check if the ?id= has been set if so continue</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// connect to the database</span>
		<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #666666; font-style: italic;">// retrieves info from the row relating to the ID</span>
		<span style="color: #000000; font-weight: bold;">function</span> retrieve_info<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//Build the query to retrieve the email, will retrieve the email by the ID</span>
		<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM people WHERE id='<span style="color: #006699; font-weight: bold;">$id</span>'&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// Run the query and do any error stuff</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MySQL Error: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// Convert the data into an array</span>
		<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// Since this is a function we need to return the data.</span>
		<span style="color: #666666; font-style: italic;">//</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// CHANGE THESE VARIABLES</span>
		<span style="color: #666666; font-style: italic;">// Replace path by your own font path</span>
		<span style="color: #000088;">$font</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;sanford.tff&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// Font size (reccomended do not change) (make divisible by 2 for better results)</span>
		<span style="color: #000088;">$font_size</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">14</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #666666; font-style: italic;">//set var</span>
		<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> retrieve_info<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// Count the amount of characters in the email</span>
		<span style="color: #000088;">$chars</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// each character is about 14 pixels so lets width a bit more</span>
		<span style="color: #000088;">$extra</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$font_size</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// calculate width by multiplying each character by the character width plus extraspace</span>
		<span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$chars</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$extra</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//height of image</span>
		<span style="color: #000088;">$height</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$font_size</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Create the acording to the calculated size of the text</span>
		<span style="color: #000088;">$im</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Create some colors (B&amp;W)</span>
		<span style="color: #000088;">$white</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$black</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
		<span style="color: #666666; font-style: italic;">// Add the text</span>
		<span style="color: #990000;">imagettftext</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #000088;">$font_size</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$height</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$black</span><span style="color: #339933;">,</span> <span style="color: #000088;">$font</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Set the content-type</span>
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: image/png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Using imagepng() results in clearer text compared with imagejpeg()</span>
		<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// if there is no $data variable then echo text</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'no data avaliable to create image'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><a href="http://www.webmediamagazine.com/wp-content/uploads/2009/08/dynamicimage.jpg"><img src="http://www.webmediamagazine.com/wp-content/uploads/2009/08/dynamicimage.jpg" alt="dynamicimage" title="dynamicimage" width="264" height="24" class="alignnone size-full wp-image-1220" /></a></p>
<p>To put it simply this script grabs the users ID name from the GET method using variables in the URL, it then calculates the size of the image by finding out the amounting of characters and multiplying them by the font size. On the white background we grab a font in the current directory to use and write the text of the email around the center of the image. Then it outputs the image, to make the browser read it as a image file we need to change the header. Thats what the header part of the script does.</p>
<p>If no $_GET var for the user ID is found then it will just echo an error.</p>
<p>
<h3>Conclusion</h3>
</p>
<p>If you want me to put up a demo just post a comment and i&#8217;ll put it up ASAP.<br />
This method can be used to track visitors, give custom user images or an array of other things. Here are the files to download:<br />
<a class="downloadlink" href="http://www.webmediamagazine.com/wp-content/plugins/download-monitor/download.php?id=2" title="Version1.0 downloaded 343 times" >Dynamic Images PHP Files (Includes Font) (343)</a></p>


<p>Related posts:<ol><li><a href='http://www.webmediamagazine.com/php/using-simplexml-to-read-parse-xml-files-in-php/' rel='bookmark' title='Permanent Link: Using SimpleXML To Read &#038; Parse XML'>Using SimpleXML To Read &#038; Parse XML</a> <small>In this tutorial I will teach you how to use...</small></li><li><a href='http://www.webmediamagazine.com/php/using-functions-effectivley-in-php/' rel='bookmark' title='Permanent Link: Using Functions Effectivley In PHP'>Using Functions Effectivley In PHP</a> <small>Today I will show you how to use functions in...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.webmediamagazine.com/php/create-dynamic-images-with-phpmysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using SimpleXML To Read &amp; Parse XML</title>
		<link>http://www.webmediamagazine.com/php/using-simplexml-to-read-parse-xml-files-in-php/</link>
		<comments>http://www.webmediamagazine.com/php/using-simplexml-to-read-parse-xml-files-in-php/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 12:49:31 +0000</pubDate>
		<dc:creator>John Owen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.webmediamagazine.com/?p=951</guid>
		<description><![CDATA[In this tutorial I will teach you how to use SimpleXML to read &#038; parse XML feeds. Before PHP5 reading XML was a mess, this all changes with SimpleXML. You will learn how to parse a standard XML RSS Feed and display it and many other functions.]]></description>
			<content:encoded><![CDATA[<p>Before we dive into SimpleXML lets first look at XML. XML (Extensible Markup Language) is a flexible text format for creating structured computer documents. It&#8217;s W3C&#8217;s recommended standard for creating formats and sharing data on the Web.</p>
<p>An XML document comprises elements, attributes, processing instructions, comments, and entities:</p>
<p><strong>Element</strong>: Text delimited by an opening and a closing tag. A tag is a name enclosed within angle brackets.</p>
<p><strong>Attribute</strong>: A piece of qualifying information for an element. An attribute consists of a name, an equals sign, and an attribute value delimited by either single-quotes or double-quotes.</p>
<p><strong>Processing instruction</strong>: The software that is reading an XML document is referred to as a processor. A processing instruction is additional information embedded in the document to inform the processor and possibly change its behaviour.</p>
<p><strong>Comment</strong>: An XML comment begins with the characters: less-than, exclamation mark, minus, minus; and ends with the characters: minus, minus, greater-than. Any text within a comment is intended for a human reader and is ignored by the processor.</p>
<p><strong>Entity</strong>: An entity is a compact form that represents other text. Entities are used to specify problematic characters and to include slabs of text defined elsewhere. An entity reference consists of an ampersand, a name, and a semi-colon.</p>
<p>Before we start using simpleXML we have to have an XML file. Heres an example we&#8217;ll be using for part of the tutorial.</p>
<h3>XML Feed</h3>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;people</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;Names&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jim<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Bob<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name3<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Sam<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name3<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/people<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Copy this and create a new XML file from it. I named it <strong>names.xml</strong>.</p>
<h3>Step 1 &#8211; Loading The File</h3>
<p>We need to load this file into PHP so use this piece of code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'names.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Step 2 &#8211; Basic Reading</h3>
<p>An XML feed is made of elements. Each element stores a value. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jim<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>To start I will show you how to just retrieve the value of one element. I will print the value of name1 to the page.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># You can also use &quot;echo&quot; its the same thing.
</span><span style="color: #666666; font-style: italic;"># Displays the value of name1 which is &quot;Jim&quot;
</span><span style="color: #b1b100;">print</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name1</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you look at the XML file, you&#8217;ll see that the &#8220;people&#8221; element has an attribute called &#8220;title&#8221; with the value of &#8220;Names&#8221;. Use this piece of code to retrieve an attribute.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># You call an attribute just like you would with an array: $array['arrayname/number']
</span><span style="color: #b1b100;">print</span> <span style="color: #000088;">$xml</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Heres the full code so far, I added an extra line to make the output more easier to understand. You can remove it if you want.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># You can also use &quot;echo&quot; its the same thing.
</span><span style="color: #666666; font-style: italic;"># Displays the value of &lt;name1&gt; which is &quot;Jim&quot;
</span><span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'names.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name1</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># Seperates outputs (easier to read &amp; understand)
</span><span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;&lt;p&gt;&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># You call an attribute just like you would with an array: $array['arrayname/number']
</span><span style="color: #b1b100;">print</span> <span style="color: #000088;">$xml</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The output would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">Jim
&nbsp;
Names</pre></div></div>

<p>Not much at the moment but this is just the basics. Now onto reading multiple items!</p>
<h3>Step 3 &#8211; Parsing Multiple Items</h3>
<p>Grab this XML data:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;people</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;People&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Bob<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Turner<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>42<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Andrew<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Reed<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>11<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;3&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Martin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Surf<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>21<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;4&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Joan<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Cliffe<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>37<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;5&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Sue<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Beach<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>45<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;6&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Gabriel<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Owen<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;7&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jack<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Truscott<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>17<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;8&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jim<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fourgorn<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>14<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;9&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Mike<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/first<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Snider<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/last<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>13<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/age<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/people<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I named my XML data &#8220;people.xml&#8221;. We will now display all three items with a few short lines of code. I will go through the code step by step.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Load the people.xml file
</span><span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'people.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># Start a foreach loop. Translation: for every &lt;item&gt; in the xml file put it into the var $item. 
</span><span style="color: #666666; font-style: italic;"># now the $item can display all the elements inside the &lt;item&gt;
</span><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;"># These three print's will display the attribute of the &lt;item&gt; (ID), display the first and last name joined together
</span>	<span style="color: #666666; font-style: italic;"># and then the age. The &lt;/br&gt; and &lt;p&gt;&lt;/p&gt; are for spacing out the results
</span>	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;ID: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Name: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">first</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">last</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/br&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Age: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">age</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;p&gt;&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this chunk of code there is actually only 5 proper lines of code to parse a xml file that could have hundreds of entries with the &#8220;item&#8221; element. The comment&#8217;s in the code explain the code step-by-step. Here is the output:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">ID: 1
Name: Bob Turner
Age: 42
&nbsp;
ID: 2
Name: Andrew Reed
Age: 11
&nbsp;
ID: 3
Name: Martin Surf
Age: 21
&nbsp;
ID: 4
Name: Joan Cliffe
Age: 37
&nbsp;
ID: 5
Name: Sue Beach
Age: 45
&nbsp;
ID: 6
Name: Gabriel Owen
Age: 5
&nbsp;
ID: 7
Name: Jack Truscott
Age: 17
&nbsp;
ID: 8
Name: Jim Fourgorn
Age: 14
&nbsp;
ID: 9
Name: Mike Snider
Age: 13</pre></div></div>

<h3>Step 4 &#8211; Filtering</h3>
<p>Say we only want to find the item that has the id of 2? Well, this can be achieved easily by adding an IF statement inside the for loop. Heres the updated code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Load the people.xml file
</span><span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'people.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># Start a foreach loop. Translation: for every &lt;item&gt; in the xml file put it into the var $item. 
</span><span style="color: #666666; font-style: italic;"># now the $item can display all the elements inside the &lt;item&gt;
</span><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</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: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;"># These three print's will display the attribute of the &lt;item&gt; (ID), display the first and last name joined together
</span>		<span style="color: #666666; font-style: italic;"># and then the age. The &lt;/br&gt; and &lt;p&gt;&lt;/p&gt; are for spacing out the results
</span>		<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;ID: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Name: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">first</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">last</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/br&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Age: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">age</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;p&gt;&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Explanation: If the item&#8217;s attribute ID = 1 on the first item then it won&#8217;t do anything with that item so it moves onto the next one. It checks the ID to the IF statement again and realizes that the ID = 2, which makes the IF statement&#8217;s condition true. This will carry out the code in the IF statement.</p>
<p>If you want to filter but display more than one item then change the condition to this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span> OR <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Lets say we want to only find the people that are between the age of 10 and 21. To do this we will use two IF statements. Since were not comparing an attribute we have to insert a little snippet of code just before the IF statement. Here&#8217;s the full code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Load the people.xml file
</span><span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'people.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># Start a foreach loop. Translation: for every &lt;item&gt; in the xml file put it into the var $item. 
</span><span style="color: #666666; font-style: italic;"># now the $item can display all the elements inside the &lt;item&gt;
</span><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$age</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">age</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$age</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">10</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: #000088;">$age</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">21</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;"># These three print's will display the attribute of the &lt;item&gt; (ID), display the first and last name joined together
</span>			<span style="color: #666666; font-style: italic;"># and then the age. The &lt;/br&gt; and &lt;p&gt;&lt;/p&gt; are for spacing out the results
</span>			<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;ID: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Name: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">first</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">last</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/br&gt;&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Age: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">age</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;p&gt;&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The $age var is set to the age in the item element. The first IF statement checks if the age is above or equal to 10, if it is then it will pass onto the next IF statement which will check if the age is below or equal to 21. If it is then it will display the item.</p>
<p>Output:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">ID: 2
Name: Andrew Reed
Age: 11
&nbsp;
ID: 3
Name: Martin Surf
Age: 21
&nbsp;
ID: 7
Name: Jack Truscott
Age: 17
&nbsp;
ID: 8
Name: Jim Fourgorn
Age: 14
&nbsp;
ID: 9
Name: Mike Snider
Age: 13</pre></div></div>

<h3>Step 5 &#8211; Reading A Standard RSS Feed</h3>
<p>Here is a standard XML RSS Feed:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rss</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;0.91&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;channel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Rarest Animals<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://www.animalinfo.org/rarest.htm<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>A list of the world's rarest mammals.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/channel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The Addax<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://www.animalcorner.co.uk/wildlife/addax.html<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The Addax (Addax nasomaculatus), sometimes called the 'screw horn antelope', 
    	because of its twisted horns, is a large, desert dwelling member of the antelope family, 
    	closely related to the Oryx.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Ethiopian Wolf<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://en.wikipedia.org/wiki/Ethiopian_Wolf<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    The Ethiopian wolf (Canis simensis) is a carnivorous mammal of the family Canidae. 
    It is also known as the Abyssinian wolf, Abyssinian fox, red jackal, red fox, 
    Simien fox, or Simien jackal among other names.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rss<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Lets take a look at the code to read this and display it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#Load Feed
</span><span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'feed.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">#Display Feed title and description with a link to the page
</span><span style="color: #b1b100;">print</span> <span style="color: #0000ff;">'&lt;a href=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;&lt;h3&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/h3&gt;&lt;/a&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">'&lt;/br&gt;&lt;/br&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">#Display the items
</span><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;"># Display title with link to page
</span>	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">'&lt;a href=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;&lt;h4&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/h4&gt;&lt;/a&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;"># display description
</span>	<span style="color: #b1b100;">print</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;"># break up the items
</span>	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">'&lt;p&gt;&lt;/p&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This will output:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">Rarest Animals
A list of the world's rarest mammals.
&nbsp;
The Addax
The Addax (Addax nasomaculatus), sometimes called the 'screw horn antelope', because of its twisted horns, is a large, desert dwelling member of the antelope family, closely related to the Oryx.
&nbsp;
Ethiopian Wolf
The Ethiopian wolf (Canis simensis) is a carnivorous mammal of the family Canidae. It is also known as the Abyssinian wolf, Abyssinian fox, red jackal, red fox, Simien fox, or Simien jackal among other names.</pre></div></div>

<p>Now, this code is basically the same as the first foreach loop with different element names. For every item in the XML file, do this with the elements. </p>
<h3>Finish</h3>
<p>If you have any problems or questions just post a comment! Thanks, don&#8217;t forget to follow us on twitter!</p>


<p>Related posts:<ol><li><a href='http://www.webmediamagazine.com/php/create-dynamic-images-with-phpmysql/' rel='bookmark' title='Permanent Link: Create Dynamic Images With PHP/MySQL'>Create Dynamic Images With PHP/MySQL</a> <small>Learn how to generate dynamic images in PHP, some uses...</small></li><li><a href='http://www.webmediamagazine.com/php/using-functions-effectivley-in-php/' rel='bookmark' title='Permanent Link: Using Functions Effectivley In PHP'>Using Functions Effectivley In PHP</a> <small>Today I will show you how to use functions in...</small></li><li><a href='http://www.webmediamagazine.com/php/limiting-text-by-a-number-of-characters-cutting-off-at-the-end-of-a-word-strrpos/' rel='bookmark' title='Permanent Link: Limiting text by a number of characters/words (strrpos).'>Limiting text by a number of characters/words (strrpos).</a> <small>If you need to display source text, but you only...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.webmediamagazine.com/php/using-simplexml-to-read-parse-xml-files-in-php/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Using Functions Effectivley In PHP</title>
		<link>http://www.webmediamagazine.com/php/using-functions-effectivley-in-php/</link>
		<comments>http://www.webmediamagazine.com/php/using-functions-effectivley-in-php/#comments</comments>
		<pubDate>Sat, 11 Jul 2009 18:29:16 +0000</pubDate>
		<dc:creator>John Owen</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.webmediamagazine.com/?p=889</guid>
		<description><![CDATA[Today I will show you how to use functions in PHP effectivley and get the most out of them. Functions can be very useful and can save a lot of time if you have a piece of PHP code that needs to be run very often.
]]></description>
			<content:encoded><![CDATA[<p>Before we start here is a partial definition from wikipedia: <em>A function has an input and an output. The defining property of a function is that, for a given input, there is one and only one output.</em></p>
<h3>Basics</h3>
<p>Functions are very simple once you understand them, here is the structure of one:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The PHP code goes inbetween { and }.</p>
<p>Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> apple <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'I have a green apple'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see the function name is called <strong>apple</strong> and when the function is called PHP will output: <em>I have a green apple</em> using <strong>echo</strong>.</p>
<h3>More Complex</h3>
<p>Here&#8217;s a more complicated example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> apple <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// declares the variable apple and sets the text to 'Green Apple'</span>
		<span style="color: #000088;">$apple</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Green Apple'</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// returns the variable, which can be used outside the function. Example: $fruit = apple(); echo $fruit;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$apple</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this function we declare the variable $apple and set it&#8217;s value to &#8216;Green Apple&#8217;, that part is simple but what does the <strong>return $apple;</strong> bit do? Well, it lets the function have an output. Heres an example to explain better.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!-- DECLARE FUNCTIONS --&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> 
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> apple <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// declares the variable apple and sets the text to 'Green Apple'</span>
		<span style="color: #000088;">$apple</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Green Apple'</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// returns the variable, which can be used outside the function. Example: $fruit = apple(); echo $fruit;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$apple</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> show_date<span style="color: #009900;">&#40;</span><span style="color: #000088;">$show_seconds</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//if statement, if $show_seconds variable is set to true, the date will also show seconds</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$show_seconds</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'TRUE'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;F j, Y, g:i:s a&quot;</span><span style="color: #009900;">&#41;</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: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;F j, Y, g:i a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// return the date either with/without seconds.</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$date</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!-- END FUNCTIONS --&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//declare the fruit variable and set it's value using the return statement in the function, the output should be 'Green Apple'</span>
<span style="color: #000088;">$fruit</span> <span style="color: #339933;">=</span> apple<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// print out the text</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'I have a '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fruit</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This chunk of code may look a little daunting but dont fear, it just has loads of spaces in it. In the declare functions section, I have exactly the same code as before so don&#8217;t worry about that. The next piece of code is how you would execute the function.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fruit</span> <span style="color: #339933;">=</span> apple<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I declare the variable $fruit and then call the function <strong>apple</strong> which returns the variable $apple. The $apple variable&#8217;s value is &#8216;Green Apple&#8217;. The $fruit variable now has the value of &#8216;Green Apple&#8217;.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'My piece of fruit is a '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fruit</span><span style="color: #339933;">;</span></pre></div></div>

<p>This chunk of code print&#8217;s out &#8216;My piece of fruit is a Green Apple&#8217;. The little . between the text and variable add&#8217;s the two strings together.</p>
<h3>Completing The Function</h3>
<p>Now, it&#8217;s time to add parameters to the function.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> show_date<span style="color: #009900;">&#40;</span><span style="color: #000088;">$show_seconds</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//if statement, if $show_seconds variable is set to true, the date will also show seconds</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$show_seconds</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'TRUE'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;F j, Y, g:i:s a&quot;</span><span style="color: #009900;">&#41;</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: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;F j, Y, g:i a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// return the date either with/without seconds.</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$date</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I&#8217;ll explain it step-by-step.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> show_date<span style="color: #009900;">&#40;</span><span style="color: #000088;">$show_seconds</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>The function is called &#8217;show_date&#8217;, inside the brackets I set a variable for the function called $show_seconds.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$show_seconds</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'TRUE'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;F j, Y, g:i:s a&quot;</span><span style="color: #009900;">&#41;</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: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;F j, Y, g:i a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This IF statement will check if the $show_seconds variable is equal to the value of &#8216;TRUE&#8217;. If it is, the $date variable will created and it&#8217;s value set to the current date including the seconds. If the $show_seconds variable doesn&#8217;t equal &#8216;TRUE&#8217; then it will set the $date variable to the current date without the seconds.</p>

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

<p>This returns the variable of $date, so the function can be used properly. Here is how the function would be used:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> show_date<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'TRUE'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>OR</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> show_date<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FALSE'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>OR</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> show_date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The first example shows the date including the seconds because the value = TRUE, the second example wouldn&#8217;t show the seconds because the $show_seconds variable isn&#8217;t equal to TRUE and the third example would be the same as the second. It doesn&#8217;t equal TRUE.</p>
<p>You can add as much parameters as you like. Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> custom_text<span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #339933;">,</span><span style="color: #000088;">$html_tag</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// compiles a string into the variable custom, if the text was 'Welcome' and the html_tag was bold then it would look like this: &lt;strong&gt;Welcome&lt;/strong&gt;</span>
		<span style="color: #000088;">$custom</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$html_tag</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$text</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$html_tag</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// outputs the function</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$custom</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If the $text variable was &#8216;Welcome&#8217; and the variable $html_tag was &#8217;strong&#8217; then the output would look like:</p>
<p><strong>Welcome</strong></p>
<h3>General Practice</h3>
<p>It is general practice to hold your functions in a seperate file, this way your scripts don&#8217;t get clogged down as much. To use the functions in your PHP script, you can use the include or require function. It only needs to be done once and should look something 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;?php</span> <span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'functions.php'</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>To call the functions from this file you would just call the function name as you would normally. </p>
<h3>Finish</h3>
<p>Here is a redirect function, no explaining needed anymore <img src='http://www.webmediamagazine.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">function redirect($url) {
		// redirects without user header to another page. 
		echo &quot;&lt;meta http-equiv='refresh' content='0;url=&quot; . $url . &quot;'&gt;&quot;;
}
&nbsp;
&nbsp;
/* EXAMPLE FOR CALLING REDIRECT FUNCTION
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//redirects to http://www.google.com</span>
redirect<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.google.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
*/</pre></div></div>

<p>Any questions? Other tutorials you want doing? Post a comment! If I got anything wrong or missed anything please feel free to correct me <img src='http://www.webmediamagazine.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Download <a href="http://www.webmediamagazine.com/php-tutorials/using-functions/functions.zip">functions.php</a> for the functions in this tutorial!</p>


<p>Related posts:<ol><li><a href='http://www.webmediamagazine.com/php/limiting-text-by-a-number-of-characters-cutting-off-at-the-end-of-a-word-strrpos/' rel='bookmark' title='Permanent Link: Limiting text by a number of characters/words (strrpos).'>Limiting text by a number of characters/words (strrpos).</a> <small>If you need to display source text, but you only...</small></li><li><a href='http://www.webmediamagazine.com/php/create-dynamic-images-with-phpmysql/' rel='bookmark' title='Permanent Link: Create Dynamic Images With PHP/MySQL'>Create Dynamic Images With PHP/MySQL</a> <small>Learn how to generate dynamic images in PHP, some uses...</small></li><li><a href='http://www.webmediamagazine.com/php/using-simplexml-to-read-parse-xml-files-in-php/' rel='bookmark' title='Permanent Link: Using SimpleXML To Read &#038; Parse XML'>Using SimpleXML To Read &#038; Parse XML</a> <small>In this tutorial I will teach you how to use...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.webmediamagazine.com/php/using-functions-effectivley-in-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Limiting text by a number of characters/words (strrpos).</title>
		<link>http://www.webmediamagazine.com/php/limiting-text-by-a-number-of-characters-cutting-off-at-the-end-of-a-word-strrpos/</link>
		<comments>http://www.webmediamagazine.com/php/limiting-text-by-a-number-of-characters-cutting-off-at-the-end-of-a-word-strrpos/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 20:16:50 +0000</pubDate>
		<dc:creator>Danny</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.webmediamagazine.com/?p=342</guid>
		<description><![CDATA[If you need to display source text, but you only want to show an excerpt of it and you will probably use the function strrpos to cut your text by a number of characters. This can look ugly as it cuts the text mid-string. Beneath is a tutorial showing you how to cut your text [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to display source text, but you only want to show an excerpt of it and you will probably use the function strrpos to cut your text by a number of characters. This can look ugly as it cuts the text mid-string. Beneath is a tutorial showing you how to cut your text by a certain amount of characters and at the end of the word.</p>
<p><span id="more-342"></span></p>
<p>Below is the full source code with comments for cutting text at the word:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$maxchars</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">500</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Assign a maximum amount of characters</span>
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$maxchars</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Cuts of the text by a specified number of characters ($maxchars).</span>
&nbsp;
<span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #339933;">,</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;">//Looks for the last space in the text &quot; &quot;) and assigns the position number to $pos.</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pos</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//Asks if there is a space in the content.</span>
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//If so, cut the text at $pos, therefore cutting it at the end of the word.</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$content</span>...&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Add an additional &quot;...&quot; to the end of your text and print it.</span></pre></div></div>

<p>To alter this code to your needs, change &#8220;$content&#8221; to your source text or assign your source text to that variable. (Eg. $content =&#8221;Blah Blah Blah&#8221;;). To alter the max number of characters displayed, change  &#8220;$maxchars&#8221; to a different number.</p>
<p>This is a great way to chop your text neatly. Enjoy!</p>


<p>Related posts:<ol><li><a href='http://www.webmediamagazine.com/php/create-dynamic-images-with-phpmysql/' rel='bookmark' title='Permanent Link: Create Dynamic Images With PHP/MySQL'>Create Dynamic Images With PHP/MySQL</a> <small>Learn how to generate dynamic images in PHP, some uses...</small></li><li><a href='http://www.webmediamagazine.com/php/using-simplexml-to-read-parse-xml-files-in-php/' rel='bookmark' title='Permanent Link: Using SimpleXML To Read &#038; Parse XML'>Using SimpleXML To Read &#038; Parse XML</a> <small>In this tutorial I will teach you how to use...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.webmediamagazine.com/php/limiting-text-by-a-number-of-characters-cutting-off-at-the-end-of-a-word-strrpos/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Random Numbers in PHP &#8211; Random Dice Roller</title>
		<link>http://www.webmediamagazine.com/php/random-numbers-in-php-lottery-numbers-dice-roller/</link>
		<comments>http://www.webmediamagazine.com/php/random-numbers-in-php-lottery-numbers-dice-roller/#comments</comments>
		<pubDate>Thu, 28 May 2009 14:25:38 +0000</pubDate>
		<dc:creator>Danny</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.webmediamagazine.com/?p=84</guid>
		<description><![CDATA[This tutorial will take a brief look at using random numbers in PHP. I&#8217;ve used a dice rolling example to demonstrate how the rand() function can be used to select an image at random. This example can be implemented and used for other applications such as an ad rotation script.

PHP comes with a basic function [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will take a brief look at using random numbers in PHP. I&#8217;ve used a dice rolling example to demonstrate how the rand() function can be used to select an image at random. This example can be implemented and used for other applications such as an ad rotation script.</p>
<p><span id="more-84"></span></p>
<p>PHP comes with a basic function called &#8220;rand()&#8221;. It can generate random numbers, depending on what parameters you set. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">//Print a random number between 5 and 15 (including 5 and 15)</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will print a random number between 5 and 15, including 5 and 15.</p>
<p><strong>Random Dice Roller<br />
</strong></p>
<p><a title="Random Dice Roller" href="http://www.webmediamagazine.com/wp-content/uploads/2009/05/dice-roller.php" target="_blank">View online demo</a></p>
<p>This demo illustrates how to use the rand() function to select random images each time the page loads.</p>
<p>Before we get started, here are the dice images I used (Right Click -&gt;Save image as):</p>
<p><a href="http://www.webmediamagazine.com/wp-content/uploads/2009/05/dice1.png"><img class="alignnone size-full wp-image-86" style="border: 0pt none;" title="dice1" src="http://www.webmediamagazine.com/wp-content/uploads/2009/05/dice1.png" alt="dice1" width="60" height="60" /></a><a href="http://www.webmediamagazine.com/wp-content/uploads/2009/05/dice2.png"><img class="alignnone size-full wp-image-87" style="border: 0pt none;" title="dice2" src="http://www.webmediamagazine.com/wp-content/uploads/2009/05/dice2.png" alt="dice2" width="60" height="60" /></a><a href="http://www.webmediamagazine.com/wp-content/uploads/2009/05/dice3.png"><img class="alignnone size-full wp-image-88" style="border: 0pt none;" title="dice3" src="http://www.webmediamagazine.com/wp-content/uploads/2009/05/dice3.png" alt="dice3" width="60" height="60" /></a><a href="http://www.webmediamagazine.com/wp-content/uploads/2009/05/dice4.png"><img class="alignnone size-full wp-image-89" style="border: 0pt none;" title="dice4" src="http://www.webmediamagazine.com/wp-content/uploads/2009/05/dice4.png" alt="dice4" width="60" height="60" /></a><a href="http://www.webmediamagazine.com/wp-content/uploads/2009/05/dice5.png"><img class="alignnone size-full wp-image-90" style="border: 0pt none;" title="dice5" src="http://www.webmediamagazine.com/wp-content/uploads/2009/05/dice5.png" alt="dice5" width="60" height="60" /></a><a href="http://www.webmediamagazine.com/wp-content/uploads/2009/05/dice6.png"><img class="alignnone size-full wp-image-91" style="border: 0pt none;" title="dice6" src="http://www.webmediamagazine.com/wp-content/uploads/2009/05/dice6.png" alt="dice6" width="60" height="60" /></a></p>
<p>Once you have chosen your images, create an array, called &#8220;img&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">//Create an array for dice images</span>
<span style="color: #000088;">$img</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></pre></div></div>

<p>Next step is to populate your array with mulitple image files:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">//Populate array with dice images</span>
<span style="color: #000088;">$img</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;img src=&quot;dice1.png&quot; alt=&quot;&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;img src=&quot;dice2.png&quot; alt=&quot;&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;img src=&quot;dice3.png&quot; alt=&quot;&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;img src=&quot;dice4.png&quot; alt=&quot;&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;img src=&quot;dice5.png&quot; alt=&quot;&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;img src=&quot;dice6.png&quot; alt=&quot;&quot; /&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note that im storing the HTML for the images in the array.</p>
<p>Now we have our array we need to count the total images stored inside it. Reason being, because we need to generate a random number between 1 &amp; the number of images.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">//Count images in array (-1 because we don't want to include 0 in the array)</span>
<span style="color: #000088;">$max</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></div></div>

<p>The final step is to generate a random number between 1 &amp; the number of images, select the correct image and print it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">//Generate a random number between 1 and 6 - (max)</span>
<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//Assign $dice with the image chosen in the array</span>
<span style="color: #000088;">$dice</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$img</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Print the dice image</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$dice</span><span style="color: #339933;">;</span></pre></div></div>

<p>In my example I have include the option to roll the dice again. This is simply a submit button that refreshes the page. By refreshing your page the images will randomly alternate.</p>
<p>The full souce code is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">//Create an array for dice images</span>
<span style="color: #000088;">$img</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Populate array with dice images</span>
<span style="color: #000088;">$img</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;img src=&quot;dice1.png&quot; alt=&quot;&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;img src=&quot;dice2.png&quot; alt=&quot;&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;img src=&quot;dice3.png&quot; alt=&quot;&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;img src=&quot;dice4.png&quot; alt=&quot;&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;img src=&quot;dice5.png&quot; alt=&quot;&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;img src=&quot;dice6.png&quot; alt=&quot;&quot; /&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Count images in array (-1 because we don't want to include 0 in the array)</span>
<span style="color: #000088;">$max</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//Generate a random number between 1 &amp;amp; 6</span>
<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//Assign $dice with the image chosen in the array</span>
<span style="color: #000088;">$dice</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$img</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Print the dice image</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$dice</span><span style="color: #339933;">;</span></pre></div></div>

<p><a title="Random Dice Roller" href="http://www.webmediamagazine.com/wp-content/uploads/2009/05/dice-roller.php" target="_blank">View online demo</a></p>
<p>I hope this tutorial helps!</p>


<p>Related posts:<ol><li><a href='http://www.webmediamagazine.com/php/create-dynamic-images-with-phpmysql/' rel='bookmark' title='Permanent Link: Create Dynamic Images With PHP/MySQL'>Create Dynamic Images With PHP/MySQL</a> <small>Learn how to generate dynamic images in PHP, some uses...</small></li><li><a href='http://www.webmediamagazine.com/php/using-simplexml-to-read-parse-xml-files-in-php/' rel='bookmark' title='Permanent Link: Using SimpleXML To Read &#038; Parse XML'>Using SimpleXML To Read &#038; Parse XML</a> <small>In this tutorial I will teach you how to use...</small></li><li><a href='http://www.webmediamagazine.com/php/using-functions-effectivley-in-php/' rel='bookmark' title='Permanent Link: Using Functions Effectivley In PHP'>Using Functions Effectivley In PHP</a> <small>Today I will show you how to use functions in...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.webmediamagazine.com/php/random-numbers-in-php-lottery-numbers-dice-roller/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
