<?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>MyChances.net &#187; php</title>
	<atom:link href="http://www.mychances.net/blog/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mychances.net/blog</link>
	<description>Data-driven college admission prediction</description>
	<lastBuildDate>Sun, 07 Mar 2010 17:39:19 +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>Forking Daemons in PHP</title>
		<link>http://www.mychances.net/blog/2009/04/30/forking-daemons-in-php/</link>
		<comments>http://www.mychances.net/blog/2009/04/30/forking-daemons-in-php/#comments</comments>
		<pubDate>Fri, 01 May 2009 02:24:16 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[daemon]]></category>
		<category><![CDATA[fork]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.mychances.net/blog/?p=78</guid>
		<description><![CDATA[Note: this is from http://bipinb.com/making-php-program-as-daemon.htm . It has been intermittently offline, so I'm archiving it here for future reference. Read on to see the code.]]></description>
			<content:encoded><![CDATA[<img style='float: left; margin-right: 10px; border: none;' src='http://www.gravatar.com/avatar.php?gravatar_id=a6b86431a750edb0d8748e2bf5a8290d&amp;default=' alt='No Gravatar' width=80 height=80/><p>Note: this is from <a href="http://bipinb.com/making-php-program-as-daemon.htm">http://bipinb.com/making-php-program-as-daemon.htm</a> . It has been intermittently offline, so I&#8217;m archiving it here for future reference.</p>
<pre>

&lt;?php
include_once('createdb.php');
declare(ticks=1);
$pid = pcntl_fork();
if ($pid == -1) {
die("could not fork");
} else if ($pid) {
exit(); // we are the parent
} else {
// we are the child
}
// detatch from the controlling terminal
if (posix_setsid() == -1) {
die("could not detach from terminal");
}
$posid=posix_getpid();
$fp = fopen("/var/run/process.pid", "w");
fwrite($fp, $posid);
fclose($fp);
// setup signal handlers
 pcntl_signal(SIGTERM, "sig_handler");
 pcntl_signal(SIGHUP, "sig_handler");
// loop forever performing tasks
 $dbobject = new DB();
 $dbobject-&gt;getCon();
 while (1) {
// do something interesting here, here i have called a function from other flile called "createdb.php"
$dbobject-&gt;CopyCallFiles();
}
 fclose($fp);
 function sig_handler($signo)
 {
switch ($signo) {
 case SIGTERM:
 // handle shutdown tasks
 exit;
 break;
 case SIGHUP:
 // handle restart tasks
 break;
 default:
 // handle all other signals
 }
}
?&gt;</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.mychances.net%2Fblog%2F2009%2F04%2F30%2Fforking-daemons-in-php%2F&amp;linkname=Forking%20Daemons%20in%20PHP"><img src="http://www.mychances.net/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.mychances.net/blog/2009/04/30/forking-daemons-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Recursive str_replace in PHP</title>
		<link>http://www.mychances.net/blog/2008/03/20/recursive-str_replace-in-php/</link>
		<comments>http://www.mychances.net/blog/2008/03/20/recursive-str_replace-in-php/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 08:50:51 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[str_replace]]></category>

		<guid isPermaLink="false">http://www.mychances.net/blog/2008/03/20/recursive-str_replace-in-php/</guid>
		<description><![CDATA[I was looking for a recursive str_replace in php tonight and I couldn&#8217;t find any, so I wrote one. This takes the exact same parameters as str_replace, in the same order. It recursively searches the $subject, replacing $search with $replace, while keeping track of how many times ($count) it has replaced the $subject.



Example:

$count=0;
$string = "; [...]]]></description>
			<content:encoded><![CDATA[<img style='float: left; margin-right: 10px; border: none;' src='http://www.gravatar.com/avatar.php?gravatar_id=a6b86431a750edb0d8748e2bf5a8290d&amp;default=' alt='No Gravatar' width=80 height=80/><p>I was looking for a recursive str_replace in php tonight and I couldn&#8217;t find any, so I wrote one. This takes the exact same parameters as str_replace, in the same order. It recursively searches the $subject, replacing $search with $replace, while keeping track of how many times ($count) it has replaced the $subject.</p>
<pre lang="PHP">
<?
function str_replace_recursive($search,$replace,$subject,&#038;$count=0){
	$newcount=$count;
	$subject = str_replace($search,$replace,$subject,$newcount);
	$count = $newcount+$count;
	if(!(strpos($subject,$search)===false)){
		return str_replace_recursive($search,$replace,$subject,$count);
	}else{
		return $subject;
	}
}
?>
</pre>
<p><strong>Example:</strong></p>
<pre>
$count=0;
$string = "; ; I; am; ; ; ; ; become; ; ; ; ; ; ; ; ;";
string = str_replace_recursive('; ;',';',$string,$count);

echo $string;
echo $count;

Outputs:
string --> I; am; become;
count --> 12
</pre>
<p>(Note that this is not the best code to use for this situation; can you write a faster solution to this example using only plain vanilla str_replace?)</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.mychances.net%2Fblog%2F2008%2F03%2F20%2Frecursive-str_replace-in-php%2F&amp;linkname=Recursive%20str_replace%20in%20PHP"><img src="http://www.mychances.net/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.mychances.net/blog/2008/03/20/recursive-str_replace-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
