<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2russianfull.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">

<channel>
	<title>PHP в SouthPark</title>
	
	<link>http://php.southpark.com.ua</link>
	<description>Хитрости и секреты CakePHP</description>
	<pubDate>Fri, 22 May 2009 14:15:15 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<geo:lat>50.30</geo:lat><geo:long>30.28</geo:long><xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /><meta xmlns="http://pipes.yahoo.com" name="pipes" content="noprocess" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/PhpSouthpark" type="application/rss+xml" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">PhpSouthpark</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2FPhpSouthpark" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://lenta.yandex.ru/settings.xml?name=feed&amp;url=http%3A%2F%2Ffeeds.feedburner.com%2FPhpSouthpark" src="http://lenta.yandex.ru/i/addfeed.gif">?????? ? ??????.?????</feedburner:feedFlare><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>CSV/TSV-формат и PHP</title>
		<link>http://php.southpark.com.ua/2009/csvtsv-format-i-php/</link>
		<comments>http://php.southpark.com.ua/2009/csvtsv-format-i-php/#comments</comments>
		<pubDate>Fri, 22 May 2009 14:14:35 +0000</pubDate>
		<dc:creator>Владимир Лучанинов</dc:creator>
		
		<category><![CDATA[Куски кода]]></category>

		<category><![CDATA[snippets]]></category>

		<category><![CDATA[tsv]]></category>

		<guid isPermaLink="false">http://php.southpark.com.ua/?p=80</guid>
		<description><![CDATA[Я люблю Excel - править огромные таблицы, быстро анализировать данные в нём одно удовольствие. Для того, чтобы обработанные данные передать в приложения на CakePHP, я просто копирую всё в буфер и вставляю в textarea. Чтобы разбирать информацию оттуда, я использую
plain text
PHP:




$rows = tsv_array&#40;$this-&#62;data&#91;'ModelName'&#93;&#91;'textarea_field'&#93;&#41;; 






А вот и сама функция для разбора TSV.

plain text
PHP:




/**


 * Convert TSV [...]]]></description>
			<content:encoded><![CDATA[<p>Я люблю Excel - править огромные таблицы, быстро анализировать данные в нём одно удовольствие. Для того, чтобы обработанные данные передать в приложения на CakePHP, я просто копирую всё в буфер и вставляю в textarea. Чтобы разбирать информацию оттуда, я использую</p>
<div class="igBar"><span id="lphp-3"><a href="#" onclick="javascript:showPlainTxt('php-3'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-3">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$rows</span> = tsv_array<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">data</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'ModelName'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'textarea_field'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>А вот и сама функция для разбора TSV.<br />
<span id="more-80"></span></p>
<div class="igBar"><span id="lphp-4"><a href="#" onclick="javascript:showPlainTxt('php-4'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-4">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">/**</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * Convert TSV to array</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> *</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * @param string $s TSV</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * @param bool/array $keys</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * &nbsp;false - don't modify headers</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * &nbsp;true - use first row as headers</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * &nbsp;array - use it as headers</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * @return array</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> */</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">function</span> tsv_array<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$s</span>, <span style="color:#0000FF;">$keys</span>=<span style="color:#000000; font-weight:bold;">false</span>, <span style="color:#0000FF;">$delimiter</span>=<span style="color:#FF0000;">"<span style="color:#000099; font-weight:bold;">\t</span>"</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$rows</span> = <a href="http://www.php.net/explode"><span style="color:#000066;">explode</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"<span style="color:#000099; font-weight:bold;">\n</span>"</span>, r<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"<span style="color:#000099; font-weight:bold;">\r</span>"</span>, <span style="color:#FF0000;">''</span>, <a href="http://www.php.net/trim"><span style="color:#000066;">trim</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$s</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$min</span> = <span style="color:#000000; font-weight:bold;">null</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$max</span> = <span style="color:#000000; font-weight:bold;">null</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$rows</span> <span style="color:#616100;">as</span> &amp;<span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$row</span> = <a href="http://www.php.net/array_map"><span style="color:#000066;">array_map</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'trim'</span>, <a href="http://www.php.net/explode"><span style="color:#000066;">explode</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$delimiter</span>, <span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$count</span> = <a href="http://www.php.net/count"><span style="color:#000066;">count</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$min</span>===<span style="color:#000000; font-weight:bold;">null</span> || <span style="color:#0000FF;">$count</span>&lt;<span style="color:#0000FF;">$min</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$min</span> = <span style="color:#0000FF;">$count</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$max</span>===<span style="color:#000000; font-weight:bold;">null</span> || <span style="color:#0000FF;">$count</span>&gt;<span style="color:#0000FF;">$max</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$max</span> = <span style="color:#0000FF;">$count</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/unset"><span style="color:#000066;">unset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$max</span>!=<span style="color:#0000FF;">$min</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$rows</span> <span style="color:#616100;">as</span> &amp;<span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">for</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$i</span>=<a href="http://www.php.net/count"><span style="color:#000066;">count</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#0000FF;">$i</span>&lt;=<span style="color:#0000FF;">$max</span>; <span style="color:#0000FF;">$i</span>++<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#000000; font-weight:bold;">false</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/unset"><span style="color:#000066;">unset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>!<span style="color:#0000FF;">$keys</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#616100;">return</span> <span style="color:#0000FF;">$rows</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$keys</span>===<span style="color:#000000; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$keys</span> = <a href="http://www.php.net/array_shift"><span style="color:#000066;">array_shift</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$rows</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$result</span> = a<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$rows</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$item</span> = a<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">for</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$i</span>=<span style="color:#CC66CC;color:#800000;">0</span>; <span style="color:#0000FF;">$i</span>&lt;count<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#0000FF;">$i</span>++<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>!<a href="http://www.php.net/isset"><span style="color:#000066;">isset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$keys</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$i</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#616100;">continue</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$item</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$keys</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$i</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF;">$row</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$i</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$result</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF;">$item</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$result</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Для понимания CSV надо просто передавать другой $delimiter. Функция специально упрощена - по правильному надо ещё проверять на обрамление двойными кавычками - тогда допускается перенос строки внутри значений.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/PhpSouthpark?a=RpizuOi0pA4:bj2WEn4FKwM:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/PhpSouthpark?i=RpizuOi0pA4:bj2WEn4FKwM:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/PhpSouthpark/~4/RpizuOi0pA4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://php.southpark.com.ua/2009/csvtsv-format-i-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Отладка PHP с FirePHP в Firebug</title>
		<link>http://php.southpark.com.ua/2008/cakephp-firebug-firephp/</link>
		<comments>http://php.southpark.com.ua/2008/cakephp-firebug-firephp/#comments</comments>
		<pubDate>Fri, 11 Jul 2008 16:29:07 +0000</pubDate>
		<dc:creator>Владимир Лучанинов</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[debug]]></category>

		<category><![CDATA[firebug]]></category>

		<category><![CDATA[firephp]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://php.southpark.com.ua/?p=77</guid>
		<description><![CDATA[Я часто ругаюсь на print_r() и чуть более продвинутый pr() потому что выводимая информация перемешивается с тем, что должен видеть пользователь. Также раздражает просмотр объектов с огромными массивами информации. Мне не очень нравится Javascript с его дурацким синтаксисом, но, наверное, единственное, что меня в нём греет - это Firebug, которые делает отладку не столь ужасной. [...]]]></description>
			<content:encoded><![CDATA[<p>Я часто ругаюсь на print_r() и чуть более продвинутый pr() потому что выводимая информация перемешивается с тем, что должен видеть пользователь. Также раздражает просмотр объектов с огромными массивами информации. Мне не очень нравится Javascript с его дурацким синтаксисом, но, наверное, единственное, что меня в нём греет - это Firebug, которые делает отладку не столь ужасной. Приятно, когда можно сделать console.log(everything, you, want); и потом просматривать информацию в нормальном раскрывающемся дереве, а не в огромной простыне.<br />
<span id="more-77"></span></p>
<p>Для этого я даже когда-то написал <a href="/2007/vyvod-iz-php-v-firebug-helper-dlya-cakephp/">JsDebugHelper</a>, но что-то меня в нём смущало (наверное, eval <img src='http://php.southpark.com.ua/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ) и в итоге я им пользовался всего пару раз. Сегодня я нашёл более удобный способ скрестить Firebug с PHP - FirePHP.</p>
<p>FirePHP состоит из 2 частей - Extension к Firefox и PHP-класс FirePHP. Скачайте их с сайта FirePHP - <a href="http://www.firephp.org/">firephp.org</a>. Достаньте из FirePHPLibrary-FirePHPCore-x.x.x.tar.gz в папке \FirePHPCore\lib\FirePHPCore\ файл FirePHP.class.php и разархивируйте его в папку vendors вашего проекта на CakePHP.</p>
<p>Добавьте в config/bootstrap.php</p>
<div class="igBar"><span id="lphp-7"><a href="#" onclick="javascript:showPlainTxt('php-7'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-7">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">App::<span style="color:#006600;">import</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Vendor'</span>, <span style="color:#FF0000;">'FirePHP'</span>, <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'file'</span>=&gt;<span style="color:#FF0000;">'FirePHP.class.php'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">function</span> fb<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>Configure::<span style="color:#006600;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'debug'</span><span style="color:#006600; font-weight:bold;">&#41;</span>&gt;=<span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$instance</span> = FirePHP::<span style="color:#006600;">getInstance</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$args</span> = <a href="http://www.php.net/func_get_args"><span style="color:#000066;">func_get_args</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <a href="http://www.php.net/call_user_func_array"><span style="color:#000066;">call_user_func_array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$instance</span>,<span style="color:#FF0000;">'fb'</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<span style="color:#0000FF;">$args</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Теперь вы можете писать</p>
<div class="igBar"><span id="lphp-8"><a href="#" onclick="javascript:showPlainTxt('php-8'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-8">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">fb<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Hello World'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// Defaults to FirePHP::LOG</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">fb<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Log message'</span>&nbsp; ,FirePHP::<a href="http://www.php.net/log"><span style="color:#000066;">LOG</span></a><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">fb<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Info message'</span> ,FirePHP::<span style="color:#006600;">INFO</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">fb<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Warn message'</span> ,FirePHP::<span style="color:#006600;">WARN</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">fb<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Error message'</span>,FirePHP::<span style="color:#006600;">ERROR</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">fb<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Message with label'</span>,<span style="color:#FF0000;">'Label'</span>,FirePHP::<a href="http://www.php.net/log"><span style="color:#000066;">LOG</span></a><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">fb<span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'key1'</span>=&gt;<span style="color:#FF0000;">'val1'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'key2'</span>=&gt;array<span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'v1'</span>, <span style="color:#FF0000;">'v2'</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'v3'</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'TestArray'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; FirePHP::<a href="http://www.php.net/log"><span style="color:#000066;">LOG</span></a></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">function</span> test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$Arg1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; throw <span style="color:#000000; font-weight:bold;">new</span> Exception<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Test Exception'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">try <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; test<span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Hello'</span>=&gt;<span style="color:#FF0000;">'World'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> catch<span style="color:#006600; font-weight:bold;">&#40;</span>Exception <span style="color:#0000FF;">$e</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; fb<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$e</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">fb<span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'2 SQL queries took 0.06 seconds'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'SQL Statement'</span>,<span style="color:#FF0000;">'Time'</span>,<span style="color:#FF0000;">'Result'</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'SELECTFROM Foo'</span>,<span style="color:#FF0000;">'0.02'</span>,<a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'row1'</span>,<span style="color:#FF0000;">'row2'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'SELECTFROM Bar'</span>,<span style="color:#FF0000;">'0.04'</span>,<a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'row1'</span>,<span style="color:#FF0000;">'row2'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; FirePHP::<span style="color:#006600;">TABLE</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>И получите такую красивую картинку<br />
<a href='http://php.southpark.com.ua/wp-content/uploads/2008/07/firephp.gif'><img src="http://php.southpark.com.ua/wp-content/uploads/2008/07/firephp.gif" alt="FirePHP" title="FirePHP" width="500" height="392" class="alignnone size-full wp-image-78" /></a></p>
<p>Очень важная вкусность - всё передаётся не в response text, а в headers. Плюс в том, что можно отлаживать AJAX-запросы и изображения, не мусорится вывод. Недостаток - все fb() должны вызываться до того как что-то будет выведено на экран. Но недостаток не так критичен, ведь тру-программисты не выводят текст в моделях и контроллерах :). В крайнем случае, можно использовать кеширование вывода (ob_start,...).</p>
<p>Эх, если бы все ошибки CakePHP можно было бы автоматически выводить в Firebug в 2 строки: dump и trace (как Exception в примере). Было бы очень удобно.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/PhpSouthpark?a=qftTSEPYeWw:-79QDWxTADg:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/PhpSouthpark?i=qftTSEPYeWw:-79QDWxTADg:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/PhpSouthpark/~4/qftTSEPYeWw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://php.southpark.com.ua/2008/cakephp-firebug-firephp/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Вложенность в регулярных выражениях</title>
		<link>http://php.southpark.com.ua/2008/recurrent-parentheses-regex/</link>
		<comments>http://php.southpark.com.ua/2008/recurrent-parentheses-regex/#comments</comments>
		<pubDate>Wed, 07 May 2008 18:08:03 +0000</pubDate>
		<dc:creator>Владимир Лучанинов</dc:creator>
		
		<category><![CDATA[Куски кода]]></category>

		<category><![CDATA[Программирование]]></category>

		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://php.southpark.com.ua/?p=76</guid>
		<description><![CDATA[Мастера регулярных выражений могут делать настоящую магию. Вместо того, чтобы писать сложные лексические анализаторы, они могут одной строчкой сделать всю работу. Это как игра го - изучить основы очень просто, но чтобы стать настоящим профессионалом нужны годы опыта.
Допустим, есть строка
plain text
CODE:




begin someFunction&#40;"test"&#41;; end; 






Нужно выделить из неё
plain text
CODE:




&#40;"test"&#41; 






Элементарно
plain text
PHP:




$s = 'begin someFunction(&#34;test&#34;); end;';


preg_match&#40;'#\(.*?\)#', $s, [...]]]></description>
			<content:encoded><![CDATA[<p>Мастера регулярных выражений могут делать настоящую магию. Вместо того, чтобы писать сложные лексические анализаторы, они могут одной строчкой сделать всю работу. Это как игра го - изучить основы очень просто, но чтобы стать настоящим профессионалом нужны годы опыта.</p>
<p>Допустим, есть строка</p>
<div class="igBar"><span id="lcode-20"><a href="#" onclick="javascript:showPlainTxt('code-20'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-20">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">begin someFunction<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"test"</span><span style="color:#006600; font-weight:bold;">&#41;</span>; end; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Нужно выделить из неё</p>
<div class="igBar"><span id="lcode-21"><a href="#" onclick="javascript:showPlainTxt('code-21'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-21">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"test"</span><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Элементарно</p>
<div class="igBar"><span id="lphp-22"><a href="#" onclick="javascript:showPlainTxt('php-22'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-22">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$s</span> = <span style="color:#FF0000;">'begin someFunction(&quot;test&quot;); end;'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/preg_match"><span style="color:#000066;">preg_match</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'#<span style="color:#000099; font-weight:bold;">\(</span>.*?<span style="color:#000099; font-weight:bold;">\)</span>#'</span>, <span style="color:#0000FF;">$s</span>, <span style="color:#0000FF;">$m</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#000066;">print_r</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$m</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>выведет</p>
<div class="igBar"><span id="lphp-23"><a href="#" onclick="javascript:showPlainTxt('php-23'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-23">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/array"><span style="color:#000066;">Array</span></a></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"test"</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Но в боевых условиях пользователи обязательно рано или поздно добавят параметр со скобками</p>
<div class="igBar"><span id="lcode-24"><a href="#" onclick="javascript:showPlainTxt('code-24'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-24">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">begin someFunction<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"test"</span>,<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">5</span>+<span style="color:#800000;color:#800000;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span>*<span style="color:#800000;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>; end; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
<span id="more-76"></span></p>
<p>Наш код</p>
<div class="igBar"><span id="lphp-25"><a href="#" onclick="javascript:showPlainTxt('php-25'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-25">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$s</span> = <span style="color:#FF0000;">'begin someFunction(&quot;test&quot;,(5+3)*2); end;'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/preg_match"><span style="color:#000066;">preg_match</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'#<span style="color:#000099; font-weight:bold;">\(</span>.*?<span style="color:#000099; font-weight:bold;">\)</span>#'</span>, <span style="color:#0000FF;">$s</span>, <span style="color:#0000FF;">$m</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#000066;">print_r</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$m</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
выведет</p>
<div class="igBar"><span id="lcode-26"><a href="#" onclick="javascript:showPlainTxt('code-26'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-26">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Array</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"test"</span>,<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">5</span>+<span style="color:#800000;color:#800000;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>А вот вам</p>
<div class="igBar"><span id="lphp-27"><a href="#" onclick="javascript:showPlainTxt('php-27'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-27">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$s</span> = <span style="color:#FF0000;">'begin someFunction(&quot;test&quot;,(5+3)*2); end;'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/preg_match"><span style="color:#000066;">preg_match</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'#<span style="color:#000099; font-weight:bold;">\(</span>(([^<span style="color:#000099; font-weight:bold;">\(</span><span style="color:#000099; font-weight:bold;">\)</span>]+)|(?R))*<span style="color:#000099; font-weight:bold;">\)</span>#'</span>, <span style="color:#0000FF;">$s</span>, <span style="color:#0000FF;">$m</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#000066;">print_r</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$m</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Это выведет</p>
<div class="igBar"><span id="lphp-28"><a href="#" onclick="javascript:showPlainTxt('php-28'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-28">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/array"><span style="color:#000066;">Array</span></a></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"test"</span>,<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">5</span>+<span style="color:#CC66CC;color:#800000;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span>*<span style="color:#CC66CC;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; *<span style="color:#CC66CC;color:#800000;">2</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; *<span style="color:#CC66CC;color:#800000;">2</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>А теперь немного оптимизируем</p>
<div class="igBar"><span id="lphp-29"><a href="#" onclick="javascript:showPlainTxt('php-29'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-29">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/preg_match"><span style="color:#000066;">preg_match</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'#<span style="color:#000099; font-weight:bold;">\(</span> (?: (?&gt;[^<span style="color:#000099; font-weight:bold;">\(</span><span style="color:#000099; font-weight:bold;">\)</span>]+) | (?R) )* <span style="color:#000099; font-weight:bold;">\)</span>#x'</span>, <span style="color:#0000FF;">$s</span>, <span style="color:#0000FF;">$m</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Модификатор "x" даёт ставить лишние пробелы для улучшения читабельности.<br />
?> - ускоряет производительность, не проверяя одни и те же символы, если они уже где-то подошли<br />
?: - делает так, чтобы не выводилось это подвыражение (заметьте пропажу [1], [2])</p>
<div class="igBar"><span id="lphp-30"><a href="#" onclick="javascript:showPlainTxt('php-30'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-30">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/array"><span style="color:#000066;">Array</span></a></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"test"</span>,<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">5</span>+<span style="color:#CC66CC;color:#800000;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span>*<span style="color:#CC66CC;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Детальнее можно читать в мануале - <a href="http://php.net/manual/en/regexp.reference.php">http://php.net/manual/en/regexp.reference.php</a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/PhpSouthpark?a=SQmoi694ACE:avNHCM13cac:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/PhpSouthpark?i=SQmoi694ACE:avNHCM13cac:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/PhpSouthpark/~4/SQmoi694ACE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://php.southpark.com.ua/2008/recurrent-parentheses-regex/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Конфиги в формате PHP</title>
		<link>http://php.southpark.com.ua/2008/config-php/</link>
		<comments>http://php.southpark.com.ua/2008/config-php/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 17:16:09 +0000</pubDate>
		<dc:creator>Владимир Лучанинов</dc:creator>
		
		<category><![CDATA[Куски кода]]></category>

		<category><![CDATA[cake]]></category>

		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[config]]></category>

		<guid isPermaLink="false">http://php.southpark.com.ua/?p=73</guid>
		<description><![CDATA[В одной из наших программ конфигурация находится в файле config.php в таком формате:
plain text
PHP:




&#60;?


$param1 = 24*3600;


$param2 = array&#40;


&#160; &#160; 'test' =&#62; array&#40;1,2,3&#41;,


&#160; &#160; 'test2' =&#62; cos&#40;30&#41;


&#41;; 






Этот конфиг правится только умными людьми, которые знают PHP и не нужно создавать для них лишние уровни абстракций.
Задача - загрузить этот конфиг в программу. Просто сделать include('config.php') нельзя, потому [...]]]></description>
			<content:encoded><![CDATA[<p>В одной из наших программ конфигурация находится в файле config.php в таком формате:</p>
<div class="igBar"><span id="lphp-35"><a href="#" onclick="javascript:showPlainTxt('php-35'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-35">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$param1</span> = <span style="color:#CC66CC;color:#800000;">24</span>*<span style="color:#CC66CC;color:#800000;">3600</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$param2</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'test'</span> =&gt; <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">1</span>,<span style="color:#CC66CC;color:#800000;">2</span>,<span style="color:#CC66CC;color:#800000;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'test2'</span> =&gt; <a href="http://www.php.net/cos"><span style="color:#000066;">cos</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">30</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Этот конфиг правится только умными людьми, которые знают PHP и не нужно создавать для них лишние уровни абстракций.</p>
<p>Задача - загрузить этот конфиг в программу. Просто сделать include('config.php') нельзя, потому что можно нечаянно перезаписать какую-то переменную, а в конфигурационном файле писать $config['param1'], $config['param2'] слишком долго.<br />
<span id="more-73"></span></p>
<div class="igBar"><span id="lphp-36"><a href="#" onclick="javascript:showPlainTxt('php-36'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-36">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">class</span> TestConfigLoader <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#0000FF;">$config</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">function</span> loadConfig<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// make $temp defined var</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$temp</span> = <span style="color:#000000; font-weight:bold;">null</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// get list of currently defined vars</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$temp</span> = <a href="http://www.php.net/array_keys"><span style="color:#000066;">array_keys</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/get_defined_vars"><span style="color:#000066;">get_defined_vars</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// load new vars</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">include</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'config.php'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// get list of new vars</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$vars</span> = <a href="http://www.php.net/array_diff"><span style="color:#000066;">array_diff</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/array_keys"><span style="color:#000066;">array_keys</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/get_defined_vars"><span style="color:#000066;">get_defined_vars</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#0000FF;">$temp</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// save new vars to $this-&gt;config</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">config</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$vars</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$var</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">config</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$var</span><span style="color:#006600; font-weight:bold;">&#93;</span> = $<span style="color:#0000FF;">$var</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Идея в том, чтобы посмотреть, какие переменные добавились и добавить их в $this->config этого класса.</p>
<p>Как это работает:</p>
<div class="igBar"><span id="lphp-37"><a href="#" onclick="javascript:showPlainTxt('php-37'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-37">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$test</span> = <span style="color:#000000; font-weight:bold;">new</span> TestConfigLoader<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$test</span>-&gt;<span style="color:#006600;">loadConfig</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#000066;">print_r</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$test</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>выведет</p>
<div class="igBar"><span id="lphp-38"><a href="#" onclick="javascript:showPlainTxt('php-38'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-38">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">TestConfigLoader Object</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span>config<span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; <a href="http://www.php.net/array"><span style="color:#000066;">Array</span></a></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span>param1<span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; <span style="color:#CC66CC;color:#800000;">86400</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span>param2<span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; <a href="http://www.php.net/array"><span style="color:#000066;">Array</span></a></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span>test<span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; <a href="http://www.php.net/array"><span style="color:#000066;">Array</span></a></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; <span style="color:#CC66CC;color:#800000;">1</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; <span style="color:#CC66CC;color:#800000;">2</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; <span style="color:#CC66CC;color:#800000;">3</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span>test2<span style="color:#006600; font-weight:bold;">&#93;</span> =&gt; <span style="color:#CC66CC;color:#800000;">0</span>.<span style="color:#CC66CC;color:#800000;">15425144988758</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>UPDATE 29 Aug 2008</strong>: Оказывается, в CakePHP это ещё проще - <a href="http://www.debuggable.com/posts/4-cakephp-tricks:4811ad82-32dc-4a38-bc10-36304834cda3">http://www.debuggable.com/posts/4-cakephp-tricks:4811ad82-32dc-4a38-bc10-36304834cda3</a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/PhpSouthpark?a=Xni9B5j3KFE:y8rty0aSx7U:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/PhpSouthpark?i=Xni9B5j3KFE:y8rty0aSx7U:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/PhpSouthpark/~4/Xni9B5j3KFE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://php.southpark.com.ua/2008/config-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Нужен CakePHP-программист</title>
		<link>http://php.southpark.com.ua/2008/cakephp-programmer/</link>
		<comments>http://php.southpark.com.ua/2008/cakephp-programmer/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 10:07:00 +0000</pubDate>
		<dc:creator>Владимир Лучанинов</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[cake]]></category>

		<guid isPermaLink="false">http://php.southpark.com.ua/?p=72</guid>
		<description><![CDATA[Мне недавно предложили участие в проекте как раз таком, как я больше всего люблю – автоматический сбор и анализ информации, сотни тысяч записей, всякие там XML, AJAX, Google Maps и самое важное – кажется, адекватные заказчики.
К сожалению, у меня сейчас совсем нет свободного времени, и я вспомнил, что у меня есть блог, в который я [...]]]></description>
			<content:encoded><![CDATA[<p>Мне недавно предложили участие в проекте как раз таком, как я больше всего люблю – автоматический сбор и анализ информации, сотни тысяч записей, всякие там XML, AJAX, Google Maps и самое важное – кажется, адекватные заказчики.</p>
<p>К сожалению, у меня сейчас совсем нет свободного времени, и я вспомнил, что у меня есть блог, в который я давно не писал. И этот блог читают более сотни CakePHP-программистов, которым может быть интересен этот проект.</p>
<p>Меня попросили не рассказывать о деталях проекта, но вы можете отправить краткое резюме на jinis_services (собачка) yahoo.de и попросить ТЗ.</p>
<p>Кстати, если ещё кому-то нужны программеры на Cake – оставляйте комментарии. Ко мне уже не первый раз обращаются с блога, может быть, сделаю отдельную страничку для вакансий.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/PhpSouthpark?a=xkYkrnMqI5Y:ybV18R2Zypc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/PhpSouthpark?i=xkYkrnMqI5Y:ybV18R2Zypc:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/PhpSouthpark/~4/xkYkrnMqI5Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://php.southpark.com.ua/2008/cakephp-programmer/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Что делать, если скрипт съедает 50Гб памяти?</title>
		<link>http://php.southpark.com.ua/2008/chto-delat-esli-skript-sedaet-50gb-pamyati/</link>
		<comments>http://php.southpark.com.ua/2008/chto-delat-esli-skript-sedaet-50gb-pamyati/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 16:50:24 +0000</pubDate>
		<dc:creator>Владимир Лучанинов</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[Куски кода]]></category>

		<category><![CDATA[cake]]></category>

		<category><![CDATA[код]]></category>

		<guid isPermaLink="false">http://php.southpark.com.ua/2008/chto-delat-esli-skript-sedaet-50gb-pamyati/</guid>
		<description><![CDATA[Я всегда выступал за то, что в большинстве случаев надо, в первую очередь, оптимизировать время разработчика, а не пытаться написать самый быстрый и нетребовательный к ресурсам код. Если код работает медленно или съедает много памяти, то можно это иногда обходить другими путями.

В моём текущем проекте есть Customers, у которых есть много Orders. Надо эти Orders [...]]]></description>
			<content:encoded><![CDATA[<p>Я всегда выступал за то, что в большинстве случаев надо, в первую очередь, оптимизировать время разработчика, а не пытаться написать самый быстрый и нетребовательный к ресурсам код. Если код работает медленно или съедает много памяти, то можно это иногда обходить другими путями.<br />
<span id="more-69"></span></p>
<p>В моём текущем проекте есть Customers, у которых есть много Orders. Надо эти Orders обработать и вывести обработанную информацию.</p>
<div class="igBar"><span id="lphp-41"><a href="#" onclick="javascript:showPlainTxt('php-41'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-41">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$customers</span> <span style="color:#616100;">as</span> &amp;<span style="color:#0000FF;">$customer</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$id</span> = <span style="color:#0000FF;">$customer</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'Customer'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'id'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$orders</span> = <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">processOrders</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$id</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// eats a lot of memory</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$customer</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'Order'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF;">$orders</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/compact"><span style="color:#000066;">compact</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'customers'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Но функция processOrders($customerId) работает как-то странно, при каждом новом вызове съедая 100Мб памяти. И после 40 клиентов заканчивается физическая память и начинается жуткий своп.</p>
<p>Программист-идеалист разбирался бы в этой странной функции долго и нудно, смотрел, где утечка памяти, оптимизировал алгоритм. Я посмотрел мельком на эту функцию, понял, что на её понимание может уйти несколько часов. Потом заметил, что для каждого клиента обработка заказов происходит только один раз и можно просто разбить работу скрипта на части. Пусть скрипт обрабатывает 10 клиентов (съедая всего 1Гб памяти), а потом перезапускается.</p>
<div class="igBar"><span id="lphp-42"><a href="#" onclick="javascript:showPlainTxt('php-42'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-42">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$counter</span> = <span style="color:#CC66CC;color:#800000;">0</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$customers</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$customer</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$id</span> = <span style="color:#0000FF;">$customer</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'Customer'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'id'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>Cache::<span style="color:#006600;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Customer.Order.'</span>.<span style="color:#0000FF;">$id</span><span style="color:#006600; font-weight:bold;">&#41;</span>==<span style="color:#000000; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$orders</span> = <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">processOrders</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$id</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// eats a lot of memory</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Cache::<span style="color:#006600;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Customer.Order.'</span>.<span style="color:#0000FF;">$id</span>, <span style="color:#0000FF;">$orders</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>++<span style="color:#0000FF;">$counter</span>&gt;=<span style="color:#CC66CC;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color:#000066;">exit</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'refreshing...&lt;script&gt;window.location.reload(true)&lt;/script&gt;'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$customers</span> <span style="color:#616100;">as</span> &amp;<span style="color:#0000FF;">$customer</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$id</span> = <span style="color:#0000FF;">$customer</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'Customer'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'id'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$customer</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'Order'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = Cache::<span style="color:#006600;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Customer.Order.'</span>.<span style="color:#0000FF;">$id</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/compact"><span style="color:#000066;">compact</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'customers'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>При этом получаем дополнительный бонус - если надо будет обработать заказы новых клиентов, то старые не будут заново обрабатываться, так как результат закеширован.</p>
<p>Спасибо CakePHP Cache и JavaScript.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/PhpSouthpark?a=RPl_q1Xlme4:tJtC4d9dekE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/PhpSouthpark?i=RPl_q1Xlme4:tJtC4d9dekE:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/PhpSouthpark/~4/RPl_q1Xlme4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://php.southpark.com.ua/2008/chto-delat-esli-skript-sedaet-50gb-pamyati/feed/</wfw:commentRss>
		</item>
		<item>
		<title>CakePHP action из командной строки</title>
		<link>http://php.southpark.com.ua/2008/cakephp-action-iz-komandnoj-stroki/</link>
		<comments>http://php.southpark.com.ua/2008/cakephp-action-iz-komandnoj-stroki/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 13:16:21 +0000</pubDate>
		<dc:creator>Владимир Лучанинов</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[cake]]></category>

		<category><![CDATA[cli]]></category>

		<category><![CDATA[console]]></category>

		<category><![CDATA[controller]]></category>

		<category><![CDATA[cron]]></category>

		<category><![CDATA[curl]]></category>

		<category><![CDATA[код]]></category>

		<category><![CDATA[браузер]]></category>

		<guid isPermaLink="false">http://php.southpark.com.ua/2008/cakephp-action-iz-komandnoj-stroki/</guid>
		<description><![CDATA[Эх, если бы создатель PHP знал как будут мучать его "простенький скриптовый язык для домашних страниц". Я сам использую его часто для таких задач, для которых намного лучше подошли бы Perl, C# и Java. Но так как их знаю в режиме read-only, то у меня получается писать более эффективный код на PHP.

Меня в таких случаях [...]]]></description>
			<content:encoded><![CDATA[<p>Эх, если бы создатель PHP знал как будут мучать его "простенький скриптовый язык для домашних страниц". Я сам использую его часто для таких задач, для которых намного лучше подошли бы Perl, C# и Java. Но так как их знаю в режиме read-only, то у меня получается писать более эффективный код на PHP.<br />
<span id="more-67"></span></p>
<p>Меня в таких случаях раздражают 2 вещи:<br />
1. Отсутствие многопоточности<br />
В основном для загрузки/закачки одновременно нескольких файлов с нескольких серверов. Для загрузки обхожусь <a href="http://php.southpark.com.ua/2007/zagruzka-v-neskolko-potokov-s-multi_curl-i-php/">multi_curlом</a>, для закачки можно написать алгоритм с неблокирующими сокетами. То есть, при желании, можно обойтись.<br />
Для других задач можно также создавать другие процессы, но это не работает под Windows.</p>
<p>2. Проблемы с долгими скриптами<br />
Известная ошибка Request timeout возникает, если не передавать никаких данных в браузер долгое время (вроде 30 секунд). Решается тем, что периодически <a href="http://php.southpark.com.ua/2007/progressbar-dlya-cakephp-v-stile-winrar/">выводится какой-то текст</a>.</p>
<p>Я решал вторую проблему тем, что писал скрипты не в CakePHP и запускал их напрямую через php /path/to/script.php. Но ведь так хорошо было бы запускать CakePHP actions с командной строки, чтобы вешать их на cron.</p>
<p>Shells+Tasks теоретически могли бы подойти, но хочется, чтобы можно было запускать напрямую любой action.</p>
<p>Сейчас это выглядет вроде</p>
<p>45 * * * *  /usr/bin/lynx -source http://example.com/controller/action<br />
или<br />
45 * * * * /usr/bin/wget -O - -q http://example.com/controller/action</p>
<p>(скопировано с задачи обслуживания Drupal)</p>
<p>А хотелось бы<br />
45 * * * * php /home/www/example.com/index.php controller/action</p>
<p>И, оказывается, что решается это удивительно просто. В config/bootstrap.php добавляется</p>
<div class="igBar"><span id="lphp-45"><a href="#" onclick="javascript:showPlainTxt('php-45'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-45">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>!<a href="http://www.php.net/empty"><span style="color:#000066;">empty</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_SERVER</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'argv'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'url'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF;">$_SERVER</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'argv'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Configure::<span style="color:#006600;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'CLI'</span>, <span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Configure::<span style="color:#006600;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'CLI'</span>, <span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>и можно запускать actions из командной строки.</p>
<p>В начале функций, которые выводят статус для того, чтобы передать что-то в браузер добавляется проверка</p>
<div class="igBar"><span id="lphp-46"><a href="#" onclick="javascript:showPlainTxt('php-46'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-46">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>Configure::<span style="color:#006600;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'CLI'</span><span style="color:#006600; font-weight:bold;">&#41;</span>==<span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">true</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
, чтобы не мусорить stdout и не отвлекать php от основной задачи.</p>
<p>Насколько я знаю, сейчас связка Apache-PHP сделана довольно хорошо и от того, что скрипт запускается из командной строки, скорость выполнения практически не меняется, так что это просто для уверенности в том, что скрипт будет работать до конца.</p>
<p>Только не забывайте, от чьего имени выполняется скрипт. Потому что можно создать как root кучу файлов и папок, а потом apache не сможет получить к ним доступ.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/PhpSouthpark?a=Brdlqolkc8M:6X3p4Y2O4qA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/PhpSouthpark?i=Brdlqolkc8M:6X3p4Y2O4qA:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/PhpSouthpark/~4/Brdlqolkc8M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://php.southpark.com.ua/2008/cakephp-action-iz-komandnoj-stroki/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Меню с выделением текущего раздела</title>
		<link>http://php.southpark.com.ua/2008/menu-element/</link>
		<comments>http://php.southpark.com.ua/2008/menu-element/#comments</comments>
		<pubDate>Fri, 01 Feb 2008 21:32:46 +0000</pubDate>
		<dc:creator>Владимир Лучанинов</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[cake]]></category>

		<category><![CDATA[controller]]></category>

		<category><![CDATA[element]]></category>

		<category><![CDATA[menu]]></category>

		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://php.southpark.com.ua/2008/menu-element/</guid>
		<description><![CDATA[Практически на всех сайтах есть навигация в виде меню. К сожалению, в CakePHP нет хелпера, который бы помогал делать меню автоматически. Это вполне реализуемо, ведь на семантически правильно свёрстаных сайтах меню делается одинаково: &#60;ul&#62;&#60;li&#62;...

Мне надоело делать меню вручную и я написал небольшой Element, который делает одноуровневое меню, выделяя текущий раздел.
В шаблоне (например, views/layouts/default.ctp) пишется
plain text
PHP:




&#60;?=$this-&#62;renderElement&#40;'menu', [...]]]></description>
			<content:encoded><![CDATA[<p>Практически на всех сайтах есть навигация в виде меню. К сожалению, в CakePHP нет хелпера, который бы помогал делать меню автоматически. Это вполне реализуемо, ведь на семантически правильно свёрстаных сайтах меню делается одинаково: &lt;ul&gt;&lt;li&gt;...<br />
<span id="more-66"></span></p>
<p>Мне надоело делать меню вручную и я написал небольшой Element, который делает одноуровневое меню, выделяя текущий раздел.</p>
<p>В шаблоне (например, views/layouts/default.ctp) пишется</p>
<div class="igBar"><span id="lphp-51"><a href="#" onclick="javascript:showPlainTxt('php-51'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-51">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?</span>=<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">renderElement</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'menu'</span>, <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'items'</span>=&gt;array<span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'Главная'</span> =&gt; <span style="color:#FF0000;">'/'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'О компании'</span> =&gt; <span style="color:#FF0000;">'/pages/company'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'Услуги'</span> =&gt; <span style="color:#FF0000;">'/services/'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'Контакты'</span> =&gt; <span style="color:#FF0000;">'/feedbacks/add'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
, где ключи - это названия разделов, а значения - пути к разделам в формате Router (то есть array('controller'=>'feedbacks', 'action'=>'add') будет работать).</p>
<p>Это выведет на главной такой код</p>
<div class="igBar"><span id="lphp-52"><a href="#" onclick="javascript:showPlainTxt('php-52'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-52">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;div <span style="color:#000000; font-weight:bold;">class</span>=<span style="color:#FF0000;">"menu"</span>&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &lt;ul&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;li <span style="color:#000000; font-weight:bold;">class</span>=<span style="color:#FF0000;">"current"</span>&gt;Главная&lt;/li&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;li&gt;&lt;a href=<span style="color:#FF0000;">"/pages/company"</span>&gt;О компании&lt;/a&gt;&lt;/li&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;li&gt;&lt;a href=<span style="color:#FF0000;">"/services/"</span>&gt;Услуги&lt;/a&gt;&lt;/li&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;li&gt;&lt;a href=<span style="color:#FF0000;">"/feedbacks/add"</span>&gt;Контакты&lt;/a&gt;&lt;/li&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &lt;/ul&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/div&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Обёрточный div мне советуют делать наши верстальщики, чтобы было легче оформлять его как угодно с помощью CSS. Текущая страница выделена классом current и не является ссылкой. Если вы привыкли верстать по-другому, то код элемента интуитивно понятен.</p>
<p><em>/views/elements/menu.ctp</em></p>
<div class="igBar"><span id="lphp-53"><a href="#" onclick="javascript:showPlainTxt('php-53'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-53">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;div <span style="color:#000000; font-weight:bold;">class</span>=<span style="color:#FF0000;">"menu"</span>&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &lt;ul&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">&lt;?</span> <span style="color:#0000FF;">$here</span> = Router::<span style="color:#006600;">url</span><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/substr"><span style="color:#000066;">substr</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">here</span>, <a href="http://www.php.net/strlen"><span style="color:#000066;">strlen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">webroot</span><span style="color:#006600; font-weight:bold;">&#41;</span>-<span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">&lt;?</span> <span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$items</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$name</span>=&gt;<span style="color:#0000FF;">$link</span><span style="color:#006600; font-weight:bold;">&#41;</span>: <span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">&lt;?</span> <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>Router::<span style="color:#006600;">url</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$link</span><span style="color:#006600; font-weight:bold;">&#41;</span> != <span style="color:#0000FF;">$here</span><span style="color:#006600; font-weight:bold;">&#41;</span>: <span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;li&gt;&lt;?=<span style="color:#0000FF;">$html</span>-&gt;<span style="color:#006600;">link</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$name</span>, <span style="color:#0000FF;">$link</span><span style="color:#006600; font-weight:bold;">&#41;</span>?&gt;&lt;/li&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">&lt;?</span> <span style="color:#616100;">else</span>: <span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;li <span style="color:#000000; font-weight:bold;">class</span>=<span style="color:#FF0000;">"current"</span>&gt;&lt;?=<span style="color:#0000FF;">$name</span>?&gt;&lt;/li&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">&lt;?</span> <span style="color:#616100;">endif</span> <span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">&lt;?</span> endforeach <span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &lt;/ul&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/div&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Для стандартной темы CakePHP я вставляю главное меню в #header после h1 и применяю такой CSS:</p>
<div class="igBar"><span id="lcss-54"><a href="#" onclick="javascript:showPlainTxt('css-54'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">CSS:</span>
<div id="css-54">
<div class="css">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #cc00cc;">#header <span style="color: #66cc66;">&#123;</span></span>padding-<span style="color: #000000; font-weight: bold;">bottom</span>:27px<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">#header h1 <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">height</span>: <span style="color: #993333;">auto</span>; <span style="color: #000000; font-weight: bold;">float</span>:<span style="color: #000000; font-weight: bold;">left</span><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">#header div<span style="color: #6666ff;">.menu </span>ul <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">padding-top</span>:<span style="color: #cc66cc;color:#800000;">0</span>; <span style="color: #000000; font-weight: bold;">float</span>:<span style="color: #000000; font-weight: bold;">right</span><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">#header div<span style="color: #6666ff;">.menu </span>ul li <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">float</span>:<span style="color: #000000; font-weight: bold;">left</span>; <span style="color: #000000; font-weight: bold;">list-style</span>:<span style="color: #993333;">none</span>; <span style="color: #000000; font-weight: bold;">font-weight</span>:<span style="color: #993333;">bold</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">#header div<span style="color: #6666ff;">.menu </span>ul li a <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">color</span>:#dd9; <span style="color: #000000; font-weight: bold;">font-weight</span>:<span style="color: #993333;">normal</span><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">#header div<span style="color: #6666ff;">.menu </span>ul li a<span style="color: #6666ff;">.current </span><span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">color</span>:#fff; <span style="color: #000000; font-weight: bold;">text-decoration</span>:<span style="color: #993333;">none</span><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Вроде получилось в стиле Cake. Что скажете?</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/PhpSouthpark?a=XsAEbZUayqs:KpTlwkKQk-8:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/PhpSouthpark?i=XsAEbZUayqs:KpTlwkKQk-8:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/PhpSouthpark/~4/XsAEbZUayqs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://php.southpark.com.ua/2008/menu-element/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Получение ключевого слова, по которому пришли на сайт</title>
		<link>http://php.southpark.com.ua/2008/extract-keyword/</link>
		<comments>http://php.southpark.com.ua/2008/extract-keyword/#comments</comments>
		<pubDate>Wed, 30 Jan 2008 00:25:19 +0000</pubDate>
		<dc:creator>Владимир Лучанинов</dc:creator>
		
		<category><![CDATA[Куски кода]]></category>

		<category><![CDATA[keyword]]></category>

		<category><![CDATA[seo]]></category>

		<guid isPermaLink="false">http://php.southpark.com.ua/2008/extract-keyword/</guid>
		<description><![CDATA[На некоторых форумах, если перейти на них с поисковика, подсвечиваются ключевые слова, которые были заданы в поиске. Это помогает быстрее найти нужную информацию. Теоретически можно пойти дальше и немного менять страницу в завимости от категории поискового запроса посетителя.

Я написал небольшую функцию, которая определяет поисковый запрос по URL.
plain text
PHP:




echo extractKeyword&#40;'http://www.yandex.ru/yandsearch?text=php+%D0%B2+southpark&#38;lr=187'&#41;; 






выведет "php в southpark".
Пример использования:
plain text
PHP:




&#60;?


&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>На некоторых форумах, если перейти на них с поисковика, подсвечиваются ключевые слова, которые были заданы в поиске. Это помогает быстрее найти нужную информацию. Теоретически можно пойти дальше и немного менять страницу в завимости от категории поискового запроса посетителя.<br />
<span id="more-65"></span></p>
<p>Я написал небольшую функцию, которая определяет поисковый запрос по URL.</p>
<div class="igBar"><span id="lphp-58"><a href="#" onclick="javascript:showPlainTxt('php-58'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-58">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> extractKeyword<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'http://www.yandex.ru/yandsearch?text=php+%D0%B2+southpark&amp;lr=187'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>выведет "php в southpark".</p>
<p>Пример использования:</p>
<div class="igBar"><span id="lphp-59"><a href="#" onclick="javascript:showPlainTxt('php-59'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-59">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$keyword</span> = extractKeyword<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_SERVER</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'HTTP_REFERER'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>stripos<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$keyword</span>, <span style="color:#FF0000;">'southpark'</span><span style="color:#006600; font-weight:bold;">&#41;</span>!==<span style="color:#000000; font-weight:bold;">false</span> &amp;&amp; stripos<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$keyword</span>, <span style="color:#FF0000;">'php'</span><span style="color:#006600; font-weight:bold;">&#41;</span>===<span style="color:#000000; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'&lt;div class=&quot;notice&quot;&gt;Это сайт о PHP, а не о &lt;a href=&quot;http://www.southpark.org.ru/&quot;&gt;SouthPark&lt;/a&gt;&lt;/div&gt;'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Вот сама функция</p>
<div class="igBar"><span id="lphp-60"><a href="#" onclick="javascript:showPlainTxt('php-60'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-60">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">/**</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * Extract keyword from search URL</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> *</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * @param string $url ex: http://google.com/search?q=%D1%82%D0%B5%D1%81%D1%82&amp;btnG=Search</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * @return string Keyword or false</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> */</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">function</span> extractKeyword<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$url</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$searchEngines</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'google.'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'yahoo.'</span> =&gt; <span style="color:#FF0000;">'p'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'live.'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'msn.'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'aol.'</span> =&gt; <span style="color:#FF0000;">'query'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'aol.'</span> =&gt; <span style="color:#FF0000;">'encquery'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'lycos.'</span> =&gt; <span style="color:#FF0000;">'query'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'ask.'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'altavista.'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'netscape.'</span> =&gt; <span style="color:#FF0000;">'query'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'cnn.'</span> =&gt; <span style="color:#FF0000;">'query'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'looksmart.'</span> =&gt; <span style="color:#FF0000;">'qt'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'about.'</span> =&gt; <span style="color:#FF0000;">'terms'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'mamma.'</span> =&gt; <span style="color:#FF0000;">'query'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'alltheweb.'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'gigablast.'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'voila.'</span> =&gt; <span style="color:#FF0000;">'rdata'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'virgilio.'</span> =&gt; <span style="color:#FF0000;">'qs'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'baidu.'</span> =&gt; <span style="color:#FF0000;">'wd'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'alice.'</span> =&gt; <span style="color:#FF0000;">'qs'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'yandex.'</span> =&gt; <span style="color:#FF0000;">'text'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'najdi.'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'aol.'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'club-internet.'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'mama.'</span> =&gt; <span style="color:#FF0000;">'query'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'seznam.'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'search.'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'szukaj.'</span> =&gt; <span style="color:#FF0000;">'szukaj'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'szukaj.'</span> =&gt; <span style="color:#FF0000;">'qt'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'netsprint.'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'szukacz.'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'yam.'</span> =&gt; <span style="color:#FF0000;">'k'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'pchome.'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'mail.ru'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'rambler.ru'</span> =&gt; <span style="color:#FF0000;">'words'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'meta.ua'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'bigmir.net'</span> =&gt; <span style="color:#FF0000;">'q'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'aport.ru'</span> =&gt; <span style="color:#FF0000;">'r'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'a-counter'</span> =&gt; <span style="color:#FF0000;">'sub_data'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'i.ua'</span> =&gt; <span style="color:#FF0000;">'q'</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$host</span> = <a href="http://www.php.net/parse_url"><span style="color:#000066;">parse_url</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$url</span>, PHP_URL_HOST<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$query</span> = <a href="http://www.php.net/parse_url"><span style="color:#000066;">parse_url</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$url</span>, PHP_URL_QUERY<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$queryItems</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/parse_str"><span style="color:#000066;">parse_str</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$query</span>, <span style="color:#0000FF;">$queryItems</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$searchEngines</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$needle</span>=&gt;<span style="color:#0000FF;">$param</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/strpos"><span style="color:#000066;">strpos</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$host</span>, <span style="color:#0000FF;">$needle</span><span style="color:#006600; font-weight:bold;">&#41;</span>!==<span style="color:#000000; font-weight:bold;">false</span> &amp;&amp; !<a href="http://www.php.net/empty"><span style="color:#000066;">empty</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$queryItems</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$param</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <a href="http://www.php.net/urldecode"><span style="color:#000066;">urldecode</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$queryItems</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$param</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Массив $searchEngines был почти полностью одолжен из скрипта Google Analytics urchin.js. Поэтому поддерживаются все поисковики, которые сейчас определяет Google Analytics плюс добавлены несколько русских и украинских поисковиков.</p>
<p>Можно легко добавить свои любимые поисковики. Если будете добавлять, отпишитесь в комментариях, может быть, они пригодятся не только вам.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/PhpSouthpark?a=INVTt7vQJko:m2jjtsySimY:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/PhpSouthpark?i=INVTt7vQJko:m2jjtsySimY:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/PhpSouthpark/~4/INVTt7vQJko" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://php.southpark.com.ua/2008/extract-keyword/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Как сделать собственный шаблонизатор</title>
		<link>http://php.southpark.com.ua/2008/your-smarty/</link>
		<comments>http://php.southpark.com.ua/2008/your-smarty/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 18:43:28 +0000</pubDate>
		<dc:creator>Владимир Лучанинов</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[Куски кода]]></category>

		<category><![CDATA[cake]]></category>

		<category><![CDATA[smarty]]></category>

		<category><![CDATA[templates]]></category>

		<category><![CDATA[код]]></category>

		<guid isPermaLink="false">http://php.southpark.com.ua/2008/your-smarty/</guid>
		<description><![CDATA[Рано или поздно у каждого программиста возникает желание написать свой Smarty  .
Называются разные аргументы - более быстрый, гибкий, удобный и так далее.
После того, как я начал пользоваться CakePHP ко мне неожиданно пришло просветление - это было как раз то, что я хотел. Но иногда возникают задачи, когда действительно нужны простейшие шаблонизаторы и CakePHP становится [...]]]></description>
			<content:encoded><![CDATA[<p>Рано или поздно у каждого программиста возникает желание написать свой Smarty <img src='http://php.southpark.com.ua/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .<br />
Называются разные аргументы - более быстрый, гибкий, удобный и так далее.</p>
<p>После того, как я начал пользоваться CakePHP ко мне неожиданно пришло просветление - это было как раз то, что я хотел. Но иногда возникают задачи, когда действительно нужны простейшие шаблонизаторы и CakePHP становится слишком тяжёлой артиллерией.<br />
<span id="more-64"></span></p>
<p>И тут все сразу смотрят в сторону eval. Первое, что приходит в голову - написать eval(file_get_contents('template.htm')). И, конечно же, это не работает. Потом читается документация и пишется что-то вроде eval('echo("'.file_get_contents('template.htm').'");');. Но и это тоже не работает.</p>
<p>А потом наконец включается мозг и приходит понимание, что нужно учится у умных людей. Например, тех, кто написал CakePHP <img src='http://php.southpark.com.ua/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Они устанавливают нужные переменные, отключают вывод на экран, инклюдят шаблон, берут вывод из буфера и обрабатывают. Обычно даже это сильно сложно, можно переменные не устанавливать.</p>
<p>Например, чтобы</p>
<div class="igBar"><span id="lhtml-64"><a href="#" onclick="javascript:showPlainTxt('html-64'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">HTML:</span>
<div id="html-64">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html&gt;</span></a></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">&lt;head&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">&lt;title&gt;</span></a></span>{=$title}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">&lt;body&gt;</span></a></span>{=$content}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>превратилось в</p>
<div class="igBar"><span id="lhtml-65"><a href="#" onclick="javascript:showPlainTxt('html-65'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">HTML:</span>
<div id="html-65">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html&gt;</span></a></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">&lt;head&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">&lt;title&gt;</span></a></span>My homepage<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">&lt;body&gt;</span></a></span>Under construction<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>можно написать такую простенькую функцию.</p>
<div class="igBar"><span id="lphp-66"><a href="#" onclick="javascript:showPlainTxt('php-66'); return false;">plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-66">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">function</span> parsePage<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$params</span>=<a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#0000FF;">$template</span>=<span style="color:#FF0000;">'main.tpl'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/ob_start"><span style="color:#000066;">ob_start</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// echo to buffer, not screen</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">include</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$template</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$html</span> = <a href="http://www.php.net/ob_get_clean"><span style="color:#000066;">ob_get_clean</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// get buffer contents</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$params</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$key</span>=&gt;<span style="color:#0000FF;">$value</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$html</span> = <a href="http://www.php.net/str_replace"><span style="color:#000066;">str_replace</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'{=$'</span>.<span style="color:#0000FF;">$key</span>.<span style="color:#FF0000;">'}'</span>, <span style="color:#0000FF;">$value</span>, <span style="color:#0000FF;">$html</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$html</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$params</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'title'</span>=&gt;<span style="color:#FF0000;">'My homepage'</span>, <span style="color:#FF0000;">'content'</span>=&gt;<span style="color:#FF0000;">'Under construction'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$html</span> = parsePage<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$params</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$html</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Преимущество в том, что работает очень быстро, кода мало, верстальщикам можно объяснить суть шаблонов за пару минут.</p>
<p>Если вы знаете, как сделать шаблонизатор ещё проще - расскажите.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/PhpSouthpark?a=cpPqke5dZyc:kwnhBpkN1fs:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/PhpSouthpark?i=cpPqke5dZyc:kwnhBpkN1fs:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/PhpSouthpark/~4/cpPqke5dZyc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://php.southpark.com.ua/2008/your-smarty/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
