php 缓存动态内容
我制作了这个片段,展示了如何使用Last-Modified和ETag标头<br/> <br/>来优化网站的缓存。如果使用正确,这将加快您的页面加载的<br/>。
<span style="color: #808080; font-style: italic;">// Start output buffering, this will</span>
<span style="color: #808080; font-style: italic;">// catch all content so that we can </span>
<span style="color: #808080; font-style: italic;">// do some calculations</span>
<a href="http://www.php.net/ob_start"><span style="color: #000066;">ob_start</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Some example HTML</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'<html>'</span>;
<span style="color: #808080; font-style: italic;">// put your content in here:</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'<h1>Example content</h1>'</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'<ul>'</span>;
<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$x</span>=<span style="color: #cc66cc;">0</span>; <span style="color: #0000ff;">$x</span> < <span style="color: #cc66cc;">10</span>; <span style="color: #0000ff;">$x</span>++<span style="color: #66cc66;">)</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"<li>List item $x</li>"</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'</ul>'</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'</html>'</span>;
<span style="color: #808080; font-style: italic;">// or include() something here</span>
<span style="color: #808080; font-style: italic;">// Now save all the content from above into</span>
<span style="color: #808080; font-style: italic;">// a variable</span>
<span style="color: #0000ff;">$PageContent</span> = <a href="http://www.php.net/ob_get_contents"><span style="color: #000066;">ob_get_contents</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// And clear the buffer, so the</span>
<span style="color: #808080; font-style: italic;">// contents will not be submitted to </span>
<span style="color: #808080; font-style: italic;">// the client (we do that later manually)</span>
<a href="http://www.php.net/ob_end_clean"><span style="color: #000066;">ob_end_clean</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Generate unique Hash-ID by using MD5</span>
<span style="color: #0000ff;">$HashID</span> = <a href="http://www.php.net/md5"><span style="color: #000066;">md5</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$PageContent</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Specify the time when the page has</span>
<span style="color: #808080; font-style: italic;">// been changed. For example this date</span>
<span style="color: #808080; font-style: italic;">// can come from the database or any</span>
<span style="color: #808080; font-style: italic;">// file. Here we define a fixed date value:</span>
<span style="color: #0000ff;">$LastChangeTime</span> = <span style="color: #cc66cc;">1144055759</span>;
<span style="color: #808080; font-style: italic;">// Define the proxy or cache expire time </span>
<span style="color: #0000ff;">$ExpireTime</span> = <span style="color: #cc66cc;">3600</span>; <span style="color: #808080; font-style: italic;">// seconds (= one hour)</span>
<span style="color: #808080; font-style: italic;">// Get request headers:</span>
<span style="color: #0000ff;">$headers</span> = <a href="http://www.php.net/apache_request_headers"><span style="color: #000066;">apache_request_headers</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// you could also use getallheaders() or $_SERVER</span>
<span style="color: #808080; font-style: italic;">// or HTTP_SERVER_VARS </span>
<span style="color: #808080; font-style: italic;">// Add the content type of your page</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Content-Type: text/html'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Content language</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Content-language: en'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Set cache/proxy informations:</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Cache-Control: max-age='</span> . <span style="color: #0000ff;">$ExpireTime</span><span style="color: #66cc66;">)</span>; <span style="color: #808080; font-style: italic;">// must-revalidate</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Expires: '</span>.<a href="http://www.php.net/gmdate"><span style="color: #000066;">gmdate</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'D, d M Y H:i:s'</span>, <a href="http://www.php.net/time"><span style="color: #000066;">time</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>+<span style="color: #0000ff;">$ExpireTime</span><span style="color: #66cc66;">)</span>.<span style="color: #ff0000;">' GMT'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Set last modified (this helps search engines </span>
<span style="color: #808080; font-style: italic;">// and other web tools to determine if a page has</span>
<span style="color: #808080; font-style: italic;">// been updated)</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Last-Modified: '</span>.<a href="http://www.php.net/gmdate"><span style="color: #000066;">gmdate</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'D, d M Y H:i:s'</span>, <span style="color: #0000ff;">$LastChangeTime</span><span style="color: #66cc66;">)</span>.<span style="color: #ff0000;">' GMT'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Send a "ETag" which represents the content</span>
<span style="color: #808080; font-style: italic;">// (this helps browsers to determine if the page</span>
<span style="color: #808080; font-style: italic;">// has been changed or if it can be loaded from</span>
<span style="color: #808080; font-style: italic;">// the cache - this will speed up the page loading)</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'ETag: '</span> . <span style="color: #0000ff;">$HashID</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// The browser "asks" us if the requested page has</span>
<span style="color: #808080; font-style: italic;">// been changed and sends the last modified date he</span>
<span style="color: #808080; font-style: italic;">// has in it's internal cache. So now we can check</span>
<span style="color: #808080; font-style: italic;">// if the submitted time equals our internal time value.</span>
<span style="color: #808080; font-style: italic;">// If yes then the page did not get updated</span>
<span style="color: #0000ff;">$PageWasUpdated</span> = !<span style="color: #66cc66;">(</span><a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$headers</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'If-Modified-Since'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span> and
<a href="http://www.php.net/strtotime"><span style="color: #000066;">strtotime</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$headers</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'If-Modified-Since'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span> == <span style="color: #0000ff;">$LastChangeTime</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// The second possibility is that the browser sends us</span>
<span style="color: #808080; font-style: italic;">// the last Hash-ID he has. If he does we can determine</span>
<span style="color: #808080; font-style: italic;">// if he has the latest version by comparing both IDs. </span>
<span style="color: #0000ff;">$DoIDsMatch</span> = <span style="color: #66cc66;">(</span><a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$headers</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'If-None-Match'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span> and
<a href="http://www.php.net/ereg"><span style="color: #000066;">ereg</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$HashID</span>, <span style="color: #0000ff;">$headers</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'If-None-Match'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Does one of the two ways apply?</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!<span style="color: #0000ff;">$PageWasUpdated</span> or <span style="color: #0000ff;">$DoIDsMatch</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// Okay, the browser already has the</span>
<span style="color: #808080; font-style: italic;">// latest version of our page in his</span>
<span style="color: #808080; font-style: italic;">// cache. So just tell him that</span>
<span style="color: #808080; font-style: italic;">// the page was not modified and DON'T</span>
<span style="color: #808080; font-style: italic;">// send the content -> this saves bandwith and</span>
<span style="color: #808080; font-style: italic;">// speeds up the loading for the visitor</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'HTTP/1.1 304 Not Modified'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// That's all, now close the connection:</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Connection: close'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// The magical part: </span>
<span style="color: #808080; font-style: italic;">// No content here ;-) </span>
<span style="color: #808080; font-style: italic;">// Just the headers</span>
<span style="color: #66cc66;">}</span>
<span style="color: #b1b100;">else</span> <span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// Okay, the browser does not have the</span>
<span style="color: #808080; font-style: italic;">// latest version or does not have any</span>
<span style="color: #808080; font-style: italic;">// version cached. So we have to send him</span>
<span style="color: #808080; font-style: italic;">// the full page.</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'HTTP/1.1 200 OK'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Tell the browser which size the content</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Content-Length: '</span> . <a href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$PageContent</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Send the full content</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$PageContent</span>;
<span style="color: #66cc66;">}</span>
php 恢复htmlspecialchars
恢复使用htmlspecialchars()函数修复的字符
<span style="color: #000000; font-weight: bold;">function</span> restore_hsc<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$len</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #0000ff;">$val</span> = <a href="http://www.php.net/str_replace"><span style="color: #000066;">str_replace</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'&amp;'</span>, <span style="color: #ff0000;">'&'</span>, <span style="color: #0000ff;">$val</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$val</span> = <a href="http://www.php.net/str_replace"><span style="color: #000066;">str_replace</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'&ouml;'</span>, <span style="color: #ff0000;">'ö'</span>, <span style="color: #0000ff;">$val</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$val</span> = <a href="http://www.php.net/str_replace"><span style="color: #000066;">str_replace</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'&auml;'</span>, <span style="color: #ff0000;">'ä'</span>, <span style="color: #0000ff;">$val</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$val</span> = <a href="http://www.php.net/str_replace"><span style="color: #000066;">str_replace</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'&uuml;'</span>, <span style="color: #ff0000;">'ü'</span>, <span style="color: #0000ff;">$val</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$val</span> = <a href="http://www.php.net/str_replace"><span style="color: #000066;">str_replace</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'&lt;'</span>, <span style="color: #ff0000;">'<'</span>, <span style="color: #0000ff;">$val</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$val</span> = <a href="http://www.php.net/str_replace"><span style="color: #000066;">str_replace</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'&gt;'</span>, <span style="color: #ff0000;">'>'</span>, <span style="color: #0000ff;">$val</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$val</span> = <a href="http://www.php.net/str_replace"><span style="color: #000066;">str_replace</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'&quot;'</span>, <span style="color: #ff0000;">'"'</span>, <span style="color: #0000ff;">$val</span><span style="color: #66cc66;">)</span>;
<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$val</span>;
<span style="color: #66cc66;">}</span>
php 简单的cURL示例
关于如何使用cURL模块下载网站或任何其他文件的简单示例。<br /> <br />您可以找到更多<a href=\"http://www.php.net/curl_setopt\"> cURL相关PHP手册页上的选项</a>。
<span style="color: #000000; font-weight: bold;">function</span> curl_download<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$Url</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// is cURL installed yet?</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!<a href="http://www.php.net/function_exists"><span style="color: #000066;">function_exists</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'curl_init'</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Sorry cURL is not installed!'</span><span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #808080; font-style: italic;">// OK cool - then let's create a new cURL resource handle</span>
<span style="color: #0000ff;">$ch</span> = curl_init<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Now set some options (most are optional)</span>
<span style="color: #808080; font-style: italic;">// Set URL to download</span>
curl_setopt<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$ch</span>, CURLOPT_URL, <span style="color: #0000ff;">$Url</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Set a referer</span>
curl_setopt<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$ch</span>, CURLOPT_REFERER, <span style="color: #ff0000;">"http://www.example.org/yay.htm"</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// User agent</span>
curl_setopt<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$ch</span>, CURLOPT_USERAGENT, <span style="color: #ff0000;">"MozillaXYZ/1.0"</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Include header in result? (0 = yes, 1 = no)</span>
curl_setopt<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$ch</span>, CURLOPT_HEADER, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Should cURL return or print out the data? (true = return, false = print)</span>
curl_setopt<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$ch</span>, CURLOPT_RETURNTRANSFER, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Timeout in seconds</span>
curl_setopt<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$ch</span>, CURLOPT_TIMEOUT, <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Download the given URL, and return output</span>
<span style="color: #0000ff;">$output</span> = curl_exec<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$ch</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Close the cURL resource, and free system resources</span>
curl_close<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$ch</span><span style="color: #66cc66;">)</span>;
<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$output</span>;
<span style="color: #66cc66;">}</span>
php 计算行数及其出现次数
这两个函数显示如何在给定文件或字符串中创建所有行及其出现的列表。
<span style="color: #808080; font-style: italic;">/**
* LineStatisticsByFile
* Creates a list with all lines of the given file and their occurrences.
*
* @param string
* @param bool
* @return string
*/</span>
<span style="color: #000000; font-weight: bold;">function</span> LineStatisticsByFile<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$Filepath</span>, <span style="color: #0000ff;">$IgnoreCase</span>=<span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #0000ff;">$NewLine</span>=<span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!<a href="http://www.php.net/file_exists"><span style="color: #000066;">file_exists</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$Filepath</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #0000ff;">$ErrorMsg</span> = <span style="color: #ff0000;">'LineStatisticsByFile error: '</span>;
<span style="color: #0000ff;">$ErrorMsg</span> .= <span style="color: #ff0000;">'The given file '</span> . <span style="color: #0000ff;">$Filepath</span> . <span style="color: #ff0000;">' does not exist!'</span>;
<a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$ErrorMsg</span><span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #b1b100;">return</span> LineStatisticsByString<span style="color: #66cc66;">(</span><a href="http://www.php.net/file_get_contents"><span style="color: #000066;">file_get_contents</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$Filepath</span><span style="color: #66cc66;">)</span>, <span style="color: #0000ff;">$IgnoreCase</span>, <span style="color: #0000ff;">$NewLine</span><span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #808080; font-style: italic;">/**
* LineStatisticsByString
* Creates a list with all lines of the given string and their occurrences.
*
* @param string
* @param bool
* @return string
*/</span>
<span style="color: #000000; font-weight: bold;">function</span> LineStatisticsByString<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$Lines</span>, <span style="color: #0000ff;">$IgnoreCase</span>=<span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #0000ff;">$NewLine</span>=<span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><a href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$Lines</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>
<span style="color: #0000ff;">$Lines</span> = <a href="http://www.php.net/implode"><span style="color: #000066;">implode</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$NewLine</span>, <span style="color: #0000ff;">$Lines</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$Lines</span> = <a href="http://www.php.net/explode"><span style="color: #000066;">explode</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$NewLine</span>, <span style="color: #0000ff;">$Lines</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$LineArray</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Go trough all lines of the given file</span>
<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$Line</span>=<span style="color: #cc66cc;">0</span>; <span style="color: #0000ff;">$Line</span> < <a href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$Lines</span><span style="color: #66cc66;">)</span>; <span style="color: #0000ff;">$Line</span>++<span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// Trim whitespace for the current line</span>
<span style="color: #0000ff;">$CurrentLine</span> = <a href="http://www.php.net/trim"><span style="color: #000066;">trim</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$Lines</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$Line</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Skip empty lines</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$CurrentLine</span> == <span style="color: #ff0000;">''</span><span style="color: #66cc66;">)</span>
<span style="color: #b1b100;">continue</span>;
<span style="color: #808080; font-style: italic;">// Use the line contents as array key</span>
<span style="color: #0000ff;">$LineKey</span> = <span style="color: #0000ff;">$CurrentLine</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$IgnoreCase</span><span style="color: #66cc66;">)</span>
<span style="color: #0000ff;">$LineKey</span> = <a href="http://www.php.net/strtolower"><span style="color: #000066;">strtolower</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$LineKey</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Check if the array key already exists,</span>
<span style="color: #808080; font-style: italic;">// and increase the counters</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$LineArray</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$LineKey</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>
<span style="color: #0000ff;">$LineArray</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$LineKey</span><span style="color: #66cc66;">]</span> += <span style="color: #cc66cc;">1</span>;
<span style="color: #b1b100;">else</span>
<span style="color: #0000ff;">$LineArray</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$LineKey</span><span style="color: #66cc66;">]</span> = <span style="color: #cc66cc;">1</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #808080; font-style: italic;">// Sort the array</span>
<a href="http://www.php.net/arsort"><span style="color: #000066;">arsort</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$LineArray</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Create a new readable array for the output file</span>
<span style="color: #0000ff;">$NewLineArray</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #b1b100;">while</span><span style="color: #66cc66;">(</span><a href="http://www.php.net/list"><span style="color: #000066;">list</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$LineKey</span>, <span style="color: #0000ff;">$LineValue</span><span style="color: #66cc66;">)</span> = <a href="http://www.php.net/each"><span style="color: #000066;">each</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$LineArray</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #0000ff;">$NewLineArray</span><span style="color: #66cc66;">[</span><span style="color: #66cc66;">]</span> = <span style="color: #0000ff;">$LineKey</span> . <span style="color: #ff0000;">': '</span> . <span style="color: #0000ff;">$LineValue</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #808080; font-style: italic;">// Return how many lines were counted</span>
<span style="color: #b1b100;">return</span> <a href="http://www.php.net/implode"><span style="color: #000066;">implode</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span>, <span style="color: #0000ff;">$NewLineArray</span><span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span>
php Printf和sscanf的例子
此示例显示如何使用printf()和sscanf()函数。<br/>(您也可以用sprintf()替换printf()以返回结果而不是打印结果。)<br/> <br/>更详细的信息:<a href=\"http://www.php.net/sprintf/\"> php.net </a>
<span style="color: #0000ff;">$file</span> = <span style="color: #ff0000;">"test.txt"</span>; <span style="color: #0000ff;">$lines</span> = <span style="color: #cc66cc;">7</span>;
<a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"The file %s consists of %d lines<span style="color: #000099; font-weight: bold;">\n</span>"</span>, <span style="color: #0000ff;">$file</span>, <span style="color: #0000ff;">$lines</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// returns --> The file test.txt consists of 7 lines</span>
<span style="color: #808080; font-style: italic;">// padding something, prefix a string with "_"</span>
<span style="color: #0000ff;">$word</span> = <span style="color: #ff0000;">'foobar'</span>;
<a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"%'_10s<span style="color: #000099; font-weight: bold;">\n</span>"</span>, <span style="color: #0000ff;">$word</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// returns --> ____foobar</span>
<span style="color: #808080; font-style: italic;">// format a number:</span>
<span style="color: #0000ff;">$number</span> = <span style="color: #cc66cc;">100.85995</span>;
<a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"%03d<span style="color: #000099; font-weight: bold;">\n</span>"</span>, <span style="color: #0000ff;">$number</span><span style="color: #66cc66;">)</span>; <span style="color: #808080; font-style: italic;">// returns --> 100</span>
<a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"%01.2f<span style="color: #000099; font-weight: bold;">\n</span>"</span>, <span style="color: #0000ff;">$number</span><span style="color: #66cc66;">)</span>; <span style="color: #808080; font-style: italic;">// returns --> 100.86</span>
<a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"%01.3f<span style="color: #000099; font-weight: bold;">\n</span>"</span>, <span style="color: #0000ff;">$number</span><span style="color: #66cc66;">)</span>; <span style="color: #808080; font-style: italic;">// returns --> 100.860</span>
<span style="color: #808080; font-style: italic;">// parse a string with sscanf #1</span>
<a href="http://www.php.net/list"><span style="color: #000066;">list</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$number</span><span style="color: #66cc66;">)</span> = <a href="http://www.php.net/sscanf"><span style="color: #000066;">sscanf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"ID/1234567"</span>,<span style="color: #ff0000;">"ID/%d"</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"$number<span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #808080; font-style: italic;">// returns --> 1234567</span>
<span style="color: #808080; font-style: italic;">// parse a string with sscanf #2</span>
<span style="color: #0000ff;">$test</span> = <span style="color: #ff0000;">"string 1234 string 5678"</span>;
<span style="color: #0000ff;">$result</span> = <a href="http://www.php.net/sscanf"><span style="color: #000066;">sscanf</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$test</span>, <span style="color: #ff0000;">"%s %d %s %d"</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/print_r"><span style="color: #000066;">print_r</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">/*
--> returns:
Array
(
[0] => string
[1] => 1234
[2] => string
[3] => 5678
)
*/</span>
php array_walk示例
演示如何使用array_walk函数格式化数组。
<span style="color: #808080; font-style: italic;">// the test array </span>
<span style="color: #0000ff;">$array</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
<span style="color: #ff0000;">'php'</span>, <span style="color: #ff0000;">'arrays'</span>, <span style="color: #ff0000;">'are'</span>, <span style="color: #ff0000;">'cool'</span>
<span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// some variable for testing:</span>
<span style="color: #0000ff;">$some_var</span> = <span style="color: #ff0000;">'NEW'</span>;
<span style="color: #808080; font-style: italic;">// the function that get's called for each entry</span>
<span style="color: #000000; font-weight: bold;">function</span> format_array_values<span style="color: #66cc66;">(</span>&<span style="color: #0000ff;">$item</span>, <span style="color: #0000ff;">$key</span>, <span style="color: #0000ff;">$some_var</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
<span style="color: #0000ff;">$item</span> = <span style="color: #0000ff;">$some_var</span> . <span style="color: #ff0000;">": $item (KEY: $key)<br/>"</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #808080; font-style: italic;">// "walk" trough each array item and call the function:</span>
<span style="color: #808080; font-style: italic;">// "format_array_values"</span>
<a href="http://www.php.net/array_walk"><span style="color: #000066;">array_walk</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$array</span>, <span style="color: #ff0000;">'format_array_values'</span>, <span style="color: #0000ff;">$some_var</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// print the result:</span>
<a href="http://www.php.net/print_r"><span style="color: #000066;">print_r</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$array</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">/*
The output will be:
Array
(
[0] => NEW: php (KEY: 0)<br/>
[1] => NEW: arrays (KEY: 1)<br/>
[2] => NEW: are (KEY: 2)<br/>
[3] => NEW: cool (KEY: 3)<br/>
)
*/</span>
php 如何阻止多个IP地址
演示如何阻止多个IP地址
<span style="color: #808080; font-style: italic;">// Denied IP's.</span>
<span style="color: #0000ff;">$deny_ips</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
<span style="color: #ff0000;">'127.0.0.1'</span>,
<span style="color: #ff0000;">'192.168.100.1'</span>,
<span style="color: #ff0000;">'192.168.200.1'</span>,
<span style="color: #ff0000;">'192.168.300.1'</span>,
<span style="color: #ff0000;">'xxx.xxx.xxx.xxx'</span>
<span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// $deny_ips = file('blocked_ips.txt');</span>
<span style="color: #808080; font-style: italic;">// read user ip adress:</span>
<span style="color: #0000ff;">$ip</span> = <a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'REMOTE_ADDR'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span> ? <a href="http://www.php.net/trim"><span style="color: #000066;">trim</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'REMOTE_ADDR'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span> : <span style="color: #ff0000;">''</span>;
<span style="color: #808080; font-style: italic;">// search current IP in $deny_ips array</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$i</span> = <a href="http://www.php.net/array_search"><span style="color: #000066;">array_search</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$ip</span>, <span style="color: #0000ff;">$deny_ips</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> !== <span style="color: #000000; font-weight: bold;">FALSE</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// $i = contains the array key of the IP adress. </span>
<span style="color: #808080; font-style: italic;">// user is blocked:</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"Your IP adress ('$ip') was blocked!"</span>;
<a href="http://www.php.net/exit"><span style="color: #000066;">exit</span></a>;
<span style="color: #66cc66;">}</span>
<span style="color: #808080; font-style: italic;">// If we reach this section, the IP adress is valid</span>
php 日期时间设计模式微格式
此示例显示如何在PHP中实现<a href=\"http://microformats.org/wiki/datetime-design-pattern\"> Microformat日期时间设计模式</a>。
<span style="color: #000000; font-weight: bold;"><?php</span>
<span style="color: #0000ff;">$UnixTimeStamp</span> = <a href="http://www.php.net/time"><span style="color: #000066;">time</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$Title</span> = <a href="http://www.php.net/strftime"><span style="color: #000066;">strftime</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'%Y-%m-%dT%H:%M:%SZ'</span>, <span style="color: #0000ff;">$UnixTimeStamp</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$Caption</span> = <a href="http://www.php.net/strftime"><span style="color: #000066;">strftime</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'%B %d, %Y at %H:%M'</span>, <span style="color: #0000ff;">$UnixTimeStamp</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'<abbr class="date" title="'</span>. <span style="color: #0000ff;">$Title</span> .<span style="color: #ff0000;">'">'</span>. <span style="color: #0000ff;">$Caption</span> .<span style="color: #ff0000;">'</abbr>'</span>;
<span style="color: #000000; font-weight: bold;">?></span>
<!-- example result -->
<abbr <span style="color: #000000; font-weight: bold;">class</span>=<span style="color: #ff0000;">"date"</span> title=<span style="color: #ff0000;">"2007-07-25T20:15:21Z"</span>>July <span style="color: #cc66cc;">25</span>, <span style="color: #cc66cc;">2007</span> at <span style="color: #cc66cc;">20</span>:<span style="color: #cc66cc;">15</span></abbr>
php 简单的HTTP身份验证示例
演示如何使用WWW-Authenticate标头创建<br/>简单登录。
<span style="color: #808080; font-style: italic;">// Status flag:</span>
<span style="color: #0000ff;">$LoginSuccessful</span> = <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #808080; font-style: italic;">// Check username and password:</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'PHP_AUTH_USER'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span> && <a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'PHP_AUTH_PW'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #0000ff;">$Username</span> = <span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'PHP_AUTH_USER'</span><span style="color: #66cc66;">]</span>;
<span style="color: #0000ff;">$Password</span> = <span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'PHP_AUTH_PW'</span><span style="color: #66cc66;">]</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$Username</span> == <span style="color: #ff0000;">'jonas'</span> && <span style="color: #0000ff;">$Password</span> == <span style="color: #ff0000;">'foobar'</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #0000ff;">$LoginSuccessful</span> = <span style="color: #000000; font-weight: bold;">true</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #66cc66;">}</span>
<span style="color: #808080; font-style: italic;">// Login passed successful?</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!<span style="color: #0000ff;">$LoginSuccessful</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">/*
** The user gets here if:
**
** 1. The user entered incorrect login data (three times)
** --> User will see the error message from below
**
** 2. Or the user requested the page for the first time
** --> Then the 401 headers apply and the "login box" will
** be shown
*/</span>
<span style="color: #808080; font-style: italic;">// The text inside the realm section will be visible for the </span>
<span style="color: #808080; font-style: italic;">// user in the login box</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'WWW-Authenticate: Basic realm="Secret page"'</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'HTTP/1.0 401 Unauthorized'</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"Login failed!<span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #b1b100;">else</span> <span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// The user entered the correct login data, put</span>
<span style="color: #808080; font-style: italic;">// your confidential data in here: </span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'you reached the secret page!'</span>;
<span style="color: #66cc66;">}</span>
php DomXML示例
这个简短的例子展示了如何使用DomXML扩展。<br/> <br/>要尝试这个例子,你需要<a href =“http://www.php.net/manual/en/ref.dom启用.php“> DomXML扩展</a>。
<span style="color: #808080; font-style: italic;">// example HTML code: (could also come from an URL)</span>
<span style="color: #0000ff;">$html</span> = <span style="color: #ff0000;">'<html>
<head>
<title>links</title>
</head>
<body>
<a href="link1.htm" title="Link title 1" target="_blank">Link #1</a><br/>
<a href="link2.htm" title="Link title 2" target="_blank">Link #2</a><br/>
<a href="link3.htm" title="Link title 3" target="_blank">Link #3</a><br/>
</body>
</html>'</span>;
<span style="color: #808080; font-style: italic;">// check if DomXML is available:</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!<a href="http://www.php.net/function_exists"><span style="color: #000066;">function_exists</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'DomDocument'</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'DomXML extension is not available :-('</span><span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'<pre>'</span>;
<span style="color: #808080; font-style: italic;">// create new DOM object:</span>
<span style="color: #0000ff;">$dom</span> = <span style="color: #000000; font-weight: bold;">new</span> DomDocument<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// load HTML code:</span>
<span style="color: #0000ff;">$dom</span>-><span style="color: #006600;">loadHtml</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$html</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// get tags by tagname (all <a> tags / links):</span>
<span style="color: #0000ff;">$tags</span> = <span style="color: #0000ff;">$dom</span>-><span style="color: #006600;">getElementsByTagName</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// loop trough all links:</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$tags</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$a</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'<b>'</span> . <span style="color: #0000ff;">$a</span>-><span style="color: #006600;">nodeValue</span> . <span style="color: #ff0000;">'</b><br/>'</span>;
<span style="color: #808080; font-style: italic;">// does this tag have attributes:</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$a</span>-><span style="color: #006600;">hasAttributes</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// loop trough all attributes:</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$a</span>-><span style="color: #006600;">attributes</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$attribute</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'- '</span> . <span style="color: #0000ff;">$attribute</span>-><span style="color: #006600;">name</span> . <span style="color: #ff0000;">': '</span> . <span style="color: #0000ff;">$attribute</span>-><span style="color: #006600;">value</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"<br/>"</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #66cc66;">}</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"<hr/>"</span>;
<span style="color: #66cc66;">}</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'</pre>'</span>;