<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jim&#039;s Code Bucket &#187; PHP</title>
	<atom:link href="http://jamesborder.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://jamesborder.com</link>
	<description>misc code snippets</description>
	<lastBuildDate>Mon, 16 Aug 2010 15:29:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Google analytics &#8211; track click to a file</title>
		<link>http://jamesborder.com/2010/05/google-analytics-track-click-to-a-file/</link>
		<comments>http://jamesborder.com/2010/05/google-analytics-track-click-to-a-file/#comments</comments>
		<pubDate>Thu, 20 May 2010 22:57:12 +0000</pubDate>
		<dc:creator>me</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jamesborder.com/?p=539</guid>
		<description><![CDATA[&#60;html>
&#60;head>
&#60;meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
&#60;title>File to track
&#60;script type="text/javascript">
&#60;!--
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i
&#60;/script>
&#60;/head>
&#60;body onLoad="MM_goToURL('parent','pdfs/file_name_here.pdf');return document.MM_returnValue">
&#60;script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
&#60;/script>
&#60;script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-1234567-1");
	pageTracker._trackPageview();
} catch(err) {}
&#60;/script>
&#60;/body>
]]></description>
			<content:encoded><![CDATA[<p><code>&lt;html><br />
&lt;head><br />
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br />
&lt;title>File to track</title></p>
<p>&lt;script type="text/javascript"><br />
&lt;!--<br />
function MM_goToURL() { //v3.0<br />
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;<br />
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");<br />
}<br />
//--><br />
&lt;/script><br />
&lt;/head></p>
<p>&lt;body onLoad="MM_goToURL('parent','pdfs/file_name_here.pdf');return document.MM_returnValue"></p>
<p>&lt;script type="text/javascript"><br />
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");<br />
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));<br />
&lt;/script><br />
&lt;script type="text/javascript"><br />
try{<br />
var pageTracker = _gat._getTracker("UA-1234567-1");<br />
	pageTracker._trackPageview();<br />
} catch(err) {}<br />
&lt;/script><br />
&lt;/body></code></p>
]]></content:encoded>
			<wfw:commentRss>http://jamesborder.com/2010/05/google-analytics-track-click-to-a-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php &#8211; The execution operator</title>
		<link>http://jamesborder.com/2009/12/php-the-execution-operator/</link>
		<comments>http://jamesborder.com/2009/12/php-the-execution-operator/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 13:24:21 +0000</pubDate>
		<dc:creator>me</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jamesborder.com/?p=308</guid>
		<description><![CDATA[**** note the back ticks not single quote ******
executes the contents of the ticks as if you are at the terminal but as apache or whoever your web server is. this code basically

echo `whoami`;	// well who is this script running as
echo `pwd`;	// think whereami
echo `mkdir test`;	// make a directory
echo `mkdir test/anothertest`;	// make a directory in [...]]]></description>
			<content:encoded><![CDATA[<p>**** note the back ticks not single quote ******</p>
<p>executes the contents of the ticks as if you are at the terminal but as apache or whoever your web server is. this code basically<br />
<code><br />
echo `whoami`;	// well who is this script running as</p>
<p>echo `pwd`;	// think whereami<br />
echo `mkdir test`;	// make a directory<br />
echo `mkdir test/anothertest`;	// make a directory in the directory created<br />
echo nl2br(`ls`);	// list what is in this directory</p>
<p>`touch log.txt`;	// make a text file named log.txt</p>
<p>$date = date("F j, Y, g:i a");<br />
`echo $date > log.txt`;		// writes the $date to log.txt<br />
`echo foo >> log.txt`;		// writes foo to log.txt<br />
echo nl2br(`cat log.txt`);	// echoes what is in log.txt</p>
<p>echo nl2br(`tail /opt/local/apache2/logs/error_log`);	// echoes what is in the error_log</p>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://jamesborder.com/2009/12/php-the-execution-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP alpha numeric only strings</title>
		<link>http://jamesborder.com/2009/10/php-alpha-numeric-only-strings/</link>
		<comments>http://jamesborder.com/2009/10/php-alpha-numeric-only-strings/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 05:25:34 +0000</pubDate>
		<dc:creator>me</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jamesborder.com/?p=290</guid>
		<description><![CDATA[$str = &#8220;@ % * alpha numeric 0 &#8211; 9 only _ &#8211; +@_#()()++&#8221;;
$str = preg_replace(&#8216;/[^A-Za-z0-9]/&#8217;, &#8221;, $str);
echo $str;
returns alphanumeric09only
]]></description>
			<content:encoded><![CDATA[<p>$str = &#8220;@ % * alpha numeric 0 &#8211; 9 only _ &#8211; +@_#()()++&#8221;;<br />
$str = preg_replace(&#8216;/[^A-Za-z0-9]/&#8217;, &#8221;, $str);</p>
<p>echo $str;<br />
returns alphanumeric09only</p>
]]></content:encoded>
			<wfw:commentRss>http://jamesborder.com/2009/10/php-alpha-numeric-only-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Short tags</title>
		<link>http://jamesborder.com/2009/10/php-short-tags/</link>
		<comments>http://jamesborder.com/2009/10/php-short-tags/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 04:32:52 +0000</pubDate>
		<dc:creator>me</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jamesborder.com/?p=360</guid>
		<description><![CDATA[enable by setting the flag in .htaccess
php_flag short_open_tag on
Open php.ini ( /etc/php.ini or /usr/local/etc/php.ini), enter:
# vi php.ini
Set short_open_tag to On:
short_open_tag = On
Save and close the file. Restart webserver:
# service httpd restart
or
# /etc/init.d/httpd graceful
]]></description>
			<content:encoded><![CDATA[<p>enable by setting the flag in .htaccess</p>
<p>php_flag short_open_tag on</p>
<p>Open php.ini ( /etc/php.ini or /usr/local/etc/php.ini), enter:<br />
# vi php.ini</p>
<p>Set short_open_tag to On:<br />
short_open_tag = On</p>
<p>Save and close the file. Restart webserver:<br />
# service httpd restart<br />
or<br />
# /etc/init.d/httpd graceful</p>
]]></content:encoded>
			<wfw:commentRss>http://jamesborder.com/2009/10/php-short-tags/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wolf CMS &#8211; Clean uploaded file names</title>
		<link>http://jamesborder.com/2009/09/wolf-cms-clean-uploaded-file-names/</link>
		<comments>http://jamesborder.com/2009/09/wolf-cms-clean-uploaded-file-names/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 20:09:02 +0000</pubDate>
		<dc:creator>me</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jamesborder.com/?p=447</guid>
		<description><![CDATA[To replace spaces with underscores and then remove everything that isn&#8217;t alpha-numeric open up 
wolf/plugins/file_manager.FileManagerController.php
and replace the existing upload function (approx line 259) with this.

    public function upload() {
        $data = $_POST['upload'];
        $path = str_replace('..', '', $data['path']);
  [...]]]></description>
			<content:encoded><![CDATA[<p>To replace spaces with underscores and then remove everything that isn&#8217;t alpha-numeric open up </p>
<p>wolf/plugins/file_manager.FileManagerController.php</p>
<p>and replace the existing upload function (approx line 259) with this.<br />
<code><br />
    public function upload() {<br />
        $data = $_POST['upload'];<br />
        $path = str_replace('..', '', $data['path']);<br />
        $overwrite = isset($data['overwrite']) ? true: false;</p>
<p>        if (isset($_FILES)) {</p>
<p>			//	$file = upload_file($_FILES['upload_file']['name'], FILES_DIR.'/'.$path.'/', $_FILES['upload_file']['tmp_name'], $overwrite);<br />
			$clean_file_name = preg_replace('/ /', '_', $_FILES['upload_file']['name'] );<br />
			$clean_file_name = preg_replace('/[^A-Za-z0-9_.]/', '', $clean_file_name );</p>
<p>            $file = upload_file( $clean_file_name, FILES_DIR.'/'.$path.'/', $_FILES['upload_file']['tmp_name'], $overwrite);</p>
<p>            if ($file === false)<br />
               Flash::set('error', __('File has not been uploaded!'));<br />
        }<br />
        redirect(get_url('plugin/file_manager/browse/'.$path));<br />
    }<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://jamesborder.com/2009/09/wolf-cms-clean-uploaded-file-names/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Ternary Operator</title>
		<link>http://jamesborder.com/2009/07/the-ternary-operator/</link>
		<comments>http://jamesborder.com/2009/07/the-ternary-operator/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 14:17:28 +0000</pubDate>
		<dc:creator>me</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jamesborder.com/?p=310</guid>
		<description><![CDATA[$variable = condition ? if true : if false; 
as in:
$age = 24;
$drinkingAge = ($age > 21) ? &#8220;have a beer&#8221; : &#8220;have an iced tea&#8221;;
echo $drinkingAge; // returns have a beer
]]></description>
			<content:encoded><![CDATA[<p>$variable = condition ? if true : if false; </p>
<p>as in:</p>
<p>$age = 24;<br />
$drinkingAge = ($age > 21) ? &#8220;have a beer&#8221; : &#8220;have an iced tea&#8221;;</p>
<p>echo $drinkingAge; // returns have a beer</p>
]]></content:encoded>
			<wfw:commentRss>http://jamesborder.com/2009/07/the-ternary-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; date menus</title>
		<link>http://jamesborder.com/2009/06/php-date-menus/</link>
		<comments>http://jamesborder.com/2009/06/php-date-menus/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 03:59:08 +0000</pubDate>
		<dc:creator>me</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jamesborder.com/site/php_-_date_menus/#When:03:59:08Z</guid>
		<description><![CDATA[&#60;?php
	$today = getdate();
	$day = $today[&#39;mday&#39;];
	$month = $today[&#39;mon&#39;];
	$year = $today[&#39;year&#39;];
	$mtharr = array(&#8220;January&#8221;,&#8221;February&#8221;,&#8221;March&#8221;,&#8221;April&#8221;,
				&#8220;May&#8221;,&#8221;June&#8221;,&#8221;July&#8221;,&#8221;August&#8221;,&#8221;September&#8221;,
				&#8220;October&#8221;,&#8221;November&#8221;,&#8221;December&#8221;);
?&#62;
&#60;select name=&#8221;start_day&#8221;> 
&#60;?php
for ($i=1;$i&#60;=31;$i++) {
	echo &#8220;nt&#60;option value=&#8221;$i&#8221;"; 
	if ($i == $day) echo &#8221; selected&#8221;;
		echo &#8220;>$i&#60;/option>&#8221;; 
}
?&#62;
&#60;/select>
&#60;select name=&#8221;start_month&#8221;>
&#60;?php
for ($i=1;$i&#60;=12;$i++) {
	echo &#8220;nt&#60;option value=&#8221;$i&#8221;";
	if ($i == $month) echo &#8221; selected&#8221;;
		echo &#8220;>&#8221; . $mtharr[$i&#45;1] . &#8220;&#60;/option>&#8221;; 
}
?&#62; 
&#60;/select>
&#60;select name=&#8221;start_year&#8221;>
&#60;?php
for ($i=0;$i&#60;=9;$i++) {
	$tmp = $today[&#39;year&#39;] + $i;
	echo &#8220;nt&#60;option value=&#8221;$tmp&#8221;";
	if ($today[&#39;year&#39;] == [...]]]></description>
			<content:encoded><![CDATA[<p>&lt;?php</p>
<p>	$today = getdate();</p>
<p>	$day = $today[&#39;mday&#39;];<br />
	$month = $today[&#39;mon&#39;];<br />
	$year = $today[&#39;year&#39;];<br />
	$mtharr = array(&#8220;January&#8221;,&#8221;February&#8221;,&#8221;March&#8221;,&#8221;April&#8221;,<br />
				&#8220;May&#8221;,&#8221;June&#8221;,&#8221;July&#8221;,&#8221;August&#8221;,&#8221;September&#8221;,<br />
				&#8220;October&#8221;,&#8221;November&#8221;,&#8221;December&#8221;);</p>
<p>?&gt;</p>
<p>&lt;select name=&#8221;start_day&#8221;> </p>
<p>&lt;?php</p>
<p>for ($i=1;$i&lt;=31;$i++) {</p>
<p>	echo &#8220;nt&lt;option value=&#8221;$i&#8221;"; </p>
<p>	if ($i == $day) echo &#8221; selected&#8221;;<br />
		echo &#8220;>$i&lt;/option>&#8221;; </p>
<p>}</p>
<p>?&gt;<br />
&lt;/select></p>
<p>&lt;select name=&#8221;start_month&#8221;><br />
&lt;?php</p>
<p>for ($i=1;$i&lt;=12;$i++) {</p>
<p>	echo &#8220;nt&lt;option value=&#8221;$i&#8221;";<br />
	if ($i == $month) echo &#8221; selected&#8221;;<br />
		echo &#8220;>&#8221; . $mtharr[$i&#45;1] . &#8220;&lt;/option>&#8221;; </p>
<p>}</p>
<p>?&gt; </p>
<p>&lt;/select></p>
<p>&lt;select name=&#8221;start_year&#8221;><br />
&lt;?php</p>
<p>for ($i=0;$i&lt;=9;$i++) {</p>
<p>	$tmp = $today[&#39;year&#39;] + $i;<br />
	echo &#8220;nt&lt;option value=&#8221;$tmp&#8221;";<br />
	if ($today[&#39;year&#39;] == $tmp) echo &#8221; selected&#8221;;<br />
		echo &#8220;>&#8221; . $tmp . &#8220;&lt;/option>&#8221;;</p>
<p>}</p>
<p>?&gt;</p>
<p>&lt;/select></p>
]]></content:encoded>
			<wfw:commentRss>http://jamesborder.com/2009/06/php-date-menus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Trim a string without breaking words</title>
		<link>http://jamesborder.com/2009/04/php-trim-a-string-without-breaking-words/</link>
		<comments>http://jamesborder.com/2009/04/php-trim-a-string-without-breaking-words/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 16:00:42 +0000</pubDate>
		<dc:creator>me</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jamesborder.com/site/php_-_trim_a_string_without_breaking_words/#When:16:00:42Z</guid>
		<description><![CDATA[neat_trim($your_text, 100);
function neat_trim( $string, $n, $close = &#39;&#8230;&#39; ) {
   $len = strlen( $string );
   if ($len > $n) {
       preg_match(&#39;/(.{&#39; . $n . &#39;}.*?)b/&#39;, $string, $matches);
       return rtrim($matches[1]) . $close;
   } else {
    [...]]]></description>
			<content:encoded><![CDATA[<p>neat_trim($your_text, 100);</p>
<p>function neat_trim( $string, $n, $close = &#39;&#8230;&#39; ) {</p>
<p>   $len = strlen( $string );</p>
<p>   if ($len > $n) {<br />
       preg_match(&#39;/(.{&#39; . $n . &#39;}.*?)b/&#39;, $string, $matches);<br />
       return rtrim($matches[1]) . $close;<br />
   } else {<br />
       return $string;<br />
   }</p>
<p>}</p>
]]></content:encoded>
			<wfw:commentRss>http://jamesborder.com/2009/04/php-trim-a-string-without-breaking-words/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Codeigniter 404 page redirect to controller</title>
		<link>http://jamesborder.com/2009/04/codeigniter-404-page-redirect-to-controller/</link>
		<comments>http://jamesborder.com/2009/04/codeigniter-404-page-redirect-to-controller/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 23:17:18 +0000</pubDate>
		<dc:creator>me</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jamesborder.com/site/codeigniter_404_page_redirect_to_controller/#When:23:17:18Z</guid>
		<description><![CDATA[look in your /system/application/errors/ and find the error_404.php and put this (edited to match your site of course)
&#60;?php
	header(&#8220;HTTP/1.1 404 Not Found&#8221;);
	header(&#8220;location:http://www.tldomain.com/index.php?/controller/sitemap/404&#8243;);
?&#62;
So when the 404 page loads the 404 still gets logged by apache etc&#8230; but once that happens it gets rushed of to (in this case) the site map. The 404 variable tells the controller [...]]]></description>
			<content:encoded><![CDATA[<p>look in your /system/application/errors/ and find the error_404.php and put this (edited to match your site of course)</p>
<p>&lt;?php<br />
	header(&#8220;HTTP/1.1 404 Not Found&#8221;);<br />
	header(&#8220;location:http://www.tldomain.com/index.php?/controller/sitemap/404&#8243;);<br />
?&gt;</p>
<p>So when the 404 page loads the 404 still gets logged by apache etc&#8230; but once that happens it gets rushed of to (in this case) the site map. The 404 variable tells the controller method in this case the site map to display a this page cannot be found message.</p>
]]></content:encoded>
			<wfw:commentRss>http://jamesborder.com/2009/04/codeigniter-404-page-redirect-to-controller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Codeigniter app log helper</title>
		<link>http://jamesborder.com/2009/04/codeigniter-app-log-helper/</link>
		<comments>http://jamesborder.com/2009/04/codeigniter-app-log-helper/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 13:03:04 +0000</pubDate>
		<dc:creator>me</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jamesborder.com/site/codeigniter_app_log_helper/#When:13:03:04Z</guid>
		<description><![CDATA[This is for debugging only. Ok, again&#8230;. this is for debugging only. If you use this REMOVE it once it goes live. At worst you forget to add the tag that clears the log file each time and it bloats horribly and your site gets progressively slower and slower, then your server will crash, you&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>This is for debugging only. Ok, again&#8230;. this is for debugging only. If you use this REMOVE it once it goes live. At worst you forget to add the tag that clears the log file each time and it bloats horribly and your site gets progressively slower and slower, then your server will crash, you&#8217;ll get fired and end up homeless. But if you remove it when your done it&#8217;s pretty handy.</p>
<p>This goes in a helper file of your choosing and make sure you load it:</p>
<p>$this&#45;>load&#45;>helper(&#39;the_helper_where_you_put_this_code&#39;);</p>
<p>Then put this in the helper file you loaded</p>
<p>if ( ! function_exists(&#39;app_log&#39;)) {</p>
<p>function app_log($arg) {</p>
<p>	// this is your log file,  so I referenced it by viewing<br />
	// domain.com/applog.htm<br />
	$log = &#39;./applog.htm&#39;;</p>
<p>	// to empty the file and start over the first call should begin with init_log<br />
	if( preg_match(&#8220;/^init_log/&#8221;, $arg) ) {</p>
<p>		// writes the the file name and whatever the arg is at the start of the file<br />
		if (!write_file($log, &#39;&#39;.$arg, &#39;w&#39;) ) {</p>
<p>			// or dies<br />
			die( &#39;Unable to write to log file&#39;);</p>
<p>		}</p>
<p>	} else {</p>
<p>		// adds whatever the $arg is to the end of the file<br />
		if ( ! write_file($log, &#39;&#39;.$arg, &#39;a+&#39;)) {</p>
<p>			// or dies<br />
			die( &#39;Unable to write to log file&#39;);</p>
<p>		}</p>
<p>	}</p>
<p>}	</p>
<p>}</p>
<p>To use the helper:</p>
<p>app_log(&#39;init_log &#45;> Whatever you want goes here&#39;);</p>
<p>I usually make the above call in the constructor, then after that in your methods whatever you want to track use:</p>
<p>app_log(&#39;blah blah blah&#39;);</p>
]]></content:encoded>
			<wfw:commentRss>http://jamesborder.com/2009/04/codeigniter-app-log-helper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
