<?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>George Papayiannis &#187; PHP/MySQL</title>
	<atom:link href="http://www.sematopia.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sematopia.com</link>
	<description></description>
	<lastBuildDate>Mon, 21 Nov 2011 23:18:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PHP: How to Redirect Server-Side (Code 301 to 307)</title>
		<link>http://www.sematopia.com/2011/04/php-how-to-redirect-server-side-code-301-to-307/</link>
		<comments>http://www.sematopia.com/2011/04/php-how-to-redirect-server-side-code-301-to-307/#comments</comments>
		<pubDate>Sat, 23 Apr 2011 21:03:03 +0000</pubDate>
		<dc:creator>George A. Papayiannis</dc:creator>
				<category><![CDATA[PHP/MySQL]]></category>

		<guid isPermaLink="false">http://www.sematopia.com/?p=644</guid>
		<description><![CDATA[I needed a way to do a server-side PHP redirect based on the RFC 2616 spec. After some research and some help on the net, I put together this: Code: function redirect($to,$code=307) { $location = null; $sn = $_SERVER['SCRIPT_NAME']; $cp = dirname($sn); if (substr($to,0,4)=='http') $location = $to; // Absolute URL else { $schema = $_SERVER['SERVER_PORT']=='443'?'https':'http'; [...]]]></description>
			<content:encoded><![CDATA[<p>I needed a way to do a server-side PHP redirect based on the <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">RFC 2616 spec</a>.  After some research and some help on the net, I put together this:</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>function redirect($to,$code=307) {
		$location = null;
		$sn = $_SERVER['SCRIPT_NAME'];
		$cp = dirname($sn);
		if (substr($to,0,4)=='http') $location = $to; // Absolute URL
		else {
			$schema = $_SERVER['SERVER_PORT']=='443'?'https':'http';
			$host = strlen($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:$_SERVER['SERVER_NAME'];
			if (substr($to,0,1)=='/') $location = "$schema://$host$to";
			elseif (substr($to,0,1)=='.') {
				$location = "$schema://$host/";
				$pu = parse_url($to);
				$cd = dirname($_SERVER['SCRIPT_FILENAME']).'/';
				$np = realpath($cd.$pu['path']);
				$np = str_replace($_SERVER['DOCUMENT_ROOT'],'',$np);
				$location.= $np;
				if ((isset($pu['query'])) &#038;&#038; (strlen($pu['query'])>0)) $location.= '?'.$pu['query'];
			}
		}

		$hs = headers_sent();
		if ($hs==false) {
			if ($code==301) header("301 Moved Permanently HTTP/1.1"); // Convert to GET
			elseif ($code==302) header("302 Found HTTP/1.1"); // Conform re-POST
			elseif ($code==303) header("303 See Other HTTP/1.1"); // dont cache, always use GET
			elseif ($code==304) header("304 Not Modified HTTP/1.1"); // use cache
			elseif ($code==305) header("305 Use Proxy HTTP/1.1");
			elseif ($code==306) header("306 Not Used HTTP/1.1");
			elseif ($code==307) header("307 Temorary Redirect HTTP/1.1");
			else trigger_error("Unhandled redirect() HTTP Code: $code",E_USER_ERROR);
			header("Location: $location");
			header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
		}
		elseif (($hs==true) || ($code==302) || ($code==303)) {
			// todo: draw some javascript to redirect
			$cover_div_style = 'background-color: #ccc; height: 100%; left: 0px; position: absolute; top: 0px; width: 100%;';
			echo "&#60;div style='$cover_div_style'>\n";
			$link_div_style = 'background-color: #fff; border: 2px solid #f00; left: 0px; margin: 5px; padding: 3px; ';
			$link_div_style.= 'position: absolute; text-align: center; top: 0px; width: 95%; z-index: 99;';
			echo "&#60;div style='$link_div_style'>\n";
			echo "&#60;p>Please See: &#60;a href='$to'>".htmlspecialchars($location)."&#60;/a>&#60;/p>\n";
			echo "&#60;/div>\n&#60;/div>\n";
		}
		exit(0);
	}</pre>
</div>
<p></code></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.sematopia.com/2011/04/php-how-to-redirect-server-side-code-301-to-307/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP: Functions to Check if String Starts &amp; Ends With String</title>
		<link>http://www.sematopia.com/2011/04/php-functions-to-check-if-string-starts-ends-with-string/</link>
		<comments>http://www.sematopia.com/2011/04/php-functions-to-check-if-string-starts-ends-with-string/#comments</comments>
		<pubDate>Sat, 23 Apr 2011 18:10:21 +0000</pubDate>
		<dc:creator>George A. Papayiannis</dc:creator>
				<category><![CDATA[PHP/MySQL]]></category>

		<guid isPermaLink="false">http://www.sematopia.com/?p=641</guid>
		<description><![CDATA[Another example of functions that aren&#8217;t part of PHP, but should be. Luckily there was a good solution on StackOverflow: Code: function startsWith($haystack,$needle,$case=true) { if($case){return (strcmp(substr($haystack, 0, strlen($needle)),$needle)===0);} return (strcasecmp(substr($haystack, 0, strlen($needle)),$needle)===0); } function endsWith($haystack,$needle,$case=true) { if($case){return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);} return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0); }]]></description>
			<content:encoded><![CDATA[<p>Another example of functions that aren&#8217;t part of PHP, but should be.  Luckily there was a good solution on <a href="http://stackoverflow.com/questions/834303/php-startswith-and-endswith-functions">StackOverflow</a>:</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>function startsWith($haystack,$needle,$case=true) {
    if($case){return (strcmp(substr($haystack, 0, strlen($needle)),$needle)===0);}
    return (strcasecmp(substr($haystack, 0, strlen($needle)),$needle)===0);
}

function endsWith($haystack,$needle,$case=true) {
    if($case){return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);}
    return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);
}</pre>
</div>
<p></code></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.sematopia.com/2011/04/php-functions-to-check-if-string-starts-ends-with-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Splitting an Array into Multiple Arrays</title>
		<link>http://www.sematopia.com/2011/04/php-splitting-an-array-into-multiple-arrays/</link>
		<comments>http://www.sematopia.com/2011/04/php-splitting-an-array-into-multiple-arrays/#comments</comments>
		<pubDate>Sat, 23 Apr 2011 18:04:56 +0000</pubDate>
		<dc:creator>George A. Papayiannis</dc:creator>
				<category><![CDATA[PHP/MySQL]]></category>

		<guid isPermaLink="false">http://www.sematopia.com/?p=639</guid>
		<description><![CDATA[I had an array that I needed to split into multiple arrays, surprised there wasn&#8217;t a built in function for this, here it is: Code: function array_split($array, $pieces=2) { if ($pieces &#60; 2) { return array($array); } $newCount = ceil(count($array)/$pieces); $a = array_slice($array, 0, $newCount); $b = $this->array_split(array_slice($array, $newCount), $pieces-1); return array_merge(array($a),$b); }]]></description>
			<content:encoded><![CDATA[<p>I had an array that I needed to split into multiple arrays, surprised there wasn&#8217;t a built in function for this,  here it is:</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>function array_split($array, $pieces=2) {
	if ($pieces &#60; 2) {
		return array($array);
	}
	$newCount = ceil(count($array)/$pieces);
	$a = array_slice($array, 0, $newCount);
	$b = $this->array_split(array_slice($array, $newCount), $pieces-1);
	return array_merge(array($a),$b);
}</pre>
</div>
<p></code></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.sematopia.com/2011/04/php-splitting-an-array-into-multiple-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using PHP to Do [Some kind] of Spelling Suggest (&#8220;Did you mean&#8221;) [PHP]</title>
		<link>http://www.sematopia.com/2011/02/using-php-to-do-some-kind-of-spell-check-did-you-mean-php/</link>
		<comments>http://www.sematopia.com/2011/02/using-php-to-do-some-kind-of-spell-check-did-you-mean-php/#comments</comments>
		<pubDate>Tue, 22 Feb 2011 20:55:13 +0000</pubDate>
		<dc:creator>George A. Papayiannis</dc:creator>
				<category><![CDATA[PHP/MySQL]]></category>

		<guid isPermaLink="false">http://www.sematopia.com/?p=626</guid>
		<description><![CDATA[I wanted to figure out a way to have a &#8220;Did you mean&#8221; spell check for search terms that didn&#8217;t return any results. By default the .dll is not available in XAMPP, so need to: 1. Install Pspell (full install) 2. Install the English dictionary on the same page 3. Copy over aspell-15.dll from C:\Program [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to figure out a way to have a &#8220;Did you mean&#8221; spell check for search terms that didn&#8217;t return any results.  By default the .dll is not available in XAMPP, so need to:</p>
<p>1. Install <a href="http://aspell.net/win32/">Pspell</a> (full install)<br />
2. Install the English dictionary on the same page<br />
3. Copy over aspell-15.dll from C:\Program Files\Aspell\bin\ to C:\xampp\apache\bin\<br />
4. Enable extension=php_pspell.dll in php.ini</p>
<p>In Debain it&#8217;s simple:</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>apt-get install php5-pspell</pre>
</div>
<p></code></div>
</div>
<p>Now to figure out some good code.. I&#8217;ll update it here if I come up with anything interesting</p>
<p><strong>Update</strong></p>
<p>Using pspell suggest alone doesn&#8217;t really give the best results.  It&#8217;s smarter to calculate the metaphone string of the word and then compare that against the suggested results.  Also, you&#8217;ll need to create your own dictionary so it doesn&#8217;t suggest a correct word.  This is basically the function I made to do that (I have a cron job to update the dictionary nightly):</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>function addString($string) {
	global $pspell_link;
	$exp = explode(" ",$string);
	foreach ($exp as $e) {
		if (strpos($e,'-') === false &#038;&#038; $e != '&#038;') {
			pspell_add_to_personal($pspell_link, rtrim($e,","));
		}
	}
}</pre>
</div>
<p></code></div>
</div>
<p>Here is the code that does the suggest, I&#8217;ve just ripped it out of a class I made, but it shows 95% of it:</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>		$result = array();
		$wordExp = explode(' ',$term);
		$i = 0;
		foreach ($wordExp as $w) {
			$wordMeta = metaphone($w);
			$newWord = '';
			if (!pspell_check($this->pspell, $w)) {
				//
				$suggest = pspell_suggest($this->pspell, $w);
				//$util->out($suggest);
				// loop all suggestions looking for the first metaphone match
				foreach ($suggest as $s) {
					// returns back case-sensative results
					if (strtolower($s) != strtolower($w)) {
						// also returns back words with spaces and dashes
						if (metaphone($s) == $wordMeta &#038;&#038; strpos($s,' ') === false &#038;&#038; strpos($s,'-') === false) {
							$newWord = $s;
							break;
						}
					}
				}
				// if no metaphone match
				if ($newWord == '') {
					foreach ($suggest as $s) {
						// just grab the first result with no space or dash
						if (strpos($s,' ') === false &#038;&#038; strpos($s,'-') === false) {
							$newWord = $s;
							break;
						}

					}
					// if there is still no new word, just return the original
					if ($newWord == '') {
						$newWord = $w;
					}
				}
			} else {
				$newWord = $w;
			}
			//
			$result[$i][0] = $w;
			$result[$i][1] = strtolower($newWord);
			$i = $i + 1;
		}
		return $result;</pre>
</div>
<p></code></div>
</div>
<p>So the function would return back and array with the original word and the suggested word:</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>Array
(
    [0] => Array
        (
            [0] => cranberrry
            [1] => cranberry
        )

)</pre>
</div>
<p></code></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.sematopia.com/2011/02/using-php-to-do-some-kind-of-spell-check-did-you-mean-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Properly Remove Special Characters a String [PHP]</title>
		<link>http://www.sematopia.com/2011/02/how-to-properly-remove-special-characters-a-string-php/</link>
		<comments>http://www.sematopia.com/2011/02/how-to-properly-remove-special-characters-a-string-php/#comments</comments>
		<pubDate>Wed, 16 Feb 2011 02:11:50 +0000</pubDate>
		<dc:creator>George A. Papayiannis</dc:creator>
				<category><![CDATA[PHP/MySQL]]></category>

		<guid isPermaLink="false">http://www.sematopia.com/?p=621</guid>
		<description><![CDATA[After getting ridiculously frustrated with all the confusing regex out there for stripping special characters from a string, I thought: &#8220;there must be a PHP way of doing this&#8221; &#8211; and then it clicked in - Code: filter_var($string, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH) That is all.]]></description>
			<content:encoded><![CDATA[<p>After getting ridiculously frustrated with all the confusing regex out there for stripping special characters from a string, I thought: &#8220;there must be a PHP way of doing this&#8221; &#8211; and then it clicked in -</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>filter_var($string, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH)</pre>
</div>
<p></code></div>
</div>
<p>That is all.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sematopia.com/2011/02/how-to-properly-remove-special-characters-a-string-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Twitter: How To (Auto) Gain Followers &amp; Influence Others (No One)</title>
		<link>http://www.sematopia.com/2010/04/twitter-how-to-auto-gain-followers-influence-others-no-one/</link>
		<comments>http://www.sematopia.com/2010/04/twitter-how-to-auto-gain-followers-influence-others-no-one/#comments</comments>
		<pubDate>Sat, 10 Apr 2010 21:35:08 +0000</pubDate>
		<dc:creator>George A. Papayiannis</dc:creator>
				<category><![CDATA[PHP/MySQL]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://www.sematopia.com/?p=565</guid>
		<description><![CDATA[Update: Don&#8217;t do this. It&#8217;s seems obvious now, but for some reason I didn&#8217;t realize this would get you banned. Indeed the Twitter account used in the experiment was banned. Luckily we explained ourselves, promised to stop and Twitter re-instated the account :) _________________________________________________________________ A couple months ago, I decided to spend an evening to [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update</strong>: Don&#8217;t do this.  It&#8217;s seems obvious now, but for some reason I didn&#8217;t realize this would get you banned.  Indeed the Twitter account used in the experiment was banned.  Luckily we explained ourselves, promised to stop and Twitter re-instated the account :)</p>
<p>_________________________________________________________________</p>
<p>A couple months ago, I decided to spend an evening to try and figure out how I could automate the finding &#038; following process on Twitter.  First off let me be clear, this wasn&#8217;t for my personal twitter account <a href="http://twitter.com/gapcm">@gapcm</a>.  God knows I don&#8217;t care about having lots of followers, nor do I care for all the self-promotion and spam that comes along with having lots of followers.  That said, there are cases were a person would want to grow their follower base, even at the expense of following a ton of people &#8211; that&#8217;s were my script comes into play!  First a little background..</p>
<p>About two years ago, my fiance and I started a site called <a href="http://thursdayfordinner.com">Thursday for Dinner</a>.  <a href="http://thursdayfordinner.com">Thursday for Dinner</a> is a video blog dedicated to preserving family recipes. Our mission, as the site says, is to capture our family&#8217;s most treasured recipes and make them available to everyone.  The Thursday for Dinner twitter account <a href="http://twitter.com/tfdtv">@tfdtv</a> would be used as the guinea pig.  Before I started this experiment, <a href="http://twitter.com/tfdtv">@tfdtv</a> had about 600 followers and was listed about 8 times.  After about 1.5 months of using my scripts <a href="http://twitter.com/tfdtv">@tfdtv</a> is up to 2160 followers and is listed 126 times.</p>
<p>The idea of the script is this: If you have API credits left and your followers count is greater than your friends count, search Twitter for a specific term (you can also randomly choose from an array of terms).  I always want to have more followers than friends (for this script), otherwise you&#8217;re looking a bit desperate.  For each user who tweeted the given term, check to see if their already in our database as recently added.  Assuming their not in the db, and that their followers to friends ratio is within a certain range, then follow them and add their user name to our database.  The ratio is calculated like this:</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>$ratio = (($followers_count - $friends_count) / $followers_count);</pre>
</div>
<p></code></div>
</div>
<p>It&#8217;s important to follow people within a specific ratio, cause if their follower to friends count is reasonably close, their more than likely going to follow you back.  More important than just the ratio, is the term you use to search Twitter.  The term you search needs to be relevant to your twitter stream/site content.  For Thursday for Dinner, I search Twitter for the term foodie.  It&#8217;s obvious that people who Tweet the word foodie, are going to like the premise of our site.  Just before the script finishes, it checks all users in the database that are older than 3 days.  If the given user is not following back, they are unfollowed.  Whether their following or not, their user name is removed from the database.</p>
<p>To use this script, your going to need to know PHP/MySQL/Apache/REST.  Your also going to need PEAR MDB2 and curl running.  If you don&#8217;t know what I&#8217;m talking about, save yourself the trouble.  First things first, you need to create a new MySQL database with the following table:</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>CREATE TABLE IF NOT EXISTS `twitter_users` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(128) NOT NULL,
  `date` datetime NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2259 ;</pre>
</div>
<p></code></div>
</div>
<p>I created a class called SessionControl to manage the whole MySQL process.  I won&#8217;t show that here, but you can download it in the zip below.  The main class is the twitter.class.php.  This is were the bulk of the work is done.</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>&#60;?

require_once 'sessionControl.class.php';

class Twitter {
    //
    private $username = 'twitter_username';
    private $password = 'twitter_password';
    //
    public function __construct() {

    }
    //
    public function getUsername() {
        return $this->username;
    }
    //
    private function getCURL() {
        if (!$curld = curl_init()) {
            echo "Could not initialize cURL session&#60;br>";
            exit();
        }
        return $curld;
    }
    //
    public function friendshipDetails($u1,$u2) {
        $url = "http://twitter.com/friendships/show.json?source_screen_name=$u1&#038;target_screen_name=$u2";
        //echo $url;
        //exit;
        $curld = $this->getCURL();
        curl_setopt($curld, CURLOPT_GET, true);
        curl_setopt($curld, CURLOPT_URL, $url);
        curl_setopt($curld, CURLOPT_HEADER, false);
        curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curld, CURLOPT_HTTPHEADER, array('Expect:'));
        $output = curl_exec($curld);
        $pile = json_decode($output,true);
        return $pile;

    }
    //
    public function follow($u) {
        $user = SessionControl::getInstance();
        //
        $url = "http://$this->username:$this->password@twitter.com/friendships/create/$u.json";
        $curld = $this->getCURL();
        curl_setopt($curld, CURLOPT_POST, true);
        curl_setopt($curld, CURLOPT_URL, $url);
        curl_setopt($curld, CURLOPT_HEADER, false);
        curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curld, CURLOPT_HTTPHEADER, array('Expect:'));
        $output = curl_exec($curld);
        //$this->out($output);
        $pile = json_decode($output,true);
        //$this->out($pile);
        //
        $sql = "INSERT INTO twitter_users (id, name, date) VALUES (NULL, ".$user->quote($u,"text").", NOW())";
        $user->query($sql);
        //
        return $pile;

    }
    //
    public function isUserInDB($u) {
        $user = SessionControl::getInstance();
        $result = false;
        $sql = 'SELECT * FROM twitter_users WHERE name = '.$user->quote($u,"text");
        $sqlRes = $user->query($sql)->fetchAll();
        if (!empty($sqlRes)) {
            $result = true;
        }
        return $result;
    }
    //
    private function destroy($u) {
        $user = SessionControl::getInstance();
        //
        $url = "http://$this->username:$this->password@twitter.com/friendships/destroy/$u.json";
        $curld = $this->getCURL();
        curl_setopt($curld, CURLOPT_POST, true);
        curl_setopt($curld, CURLOPT_URL, $url);
        curl_setopt($curld, CURLOPT_HEADER, false);
        curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curld, CURLOPT_HTTPHEADER, array('Expect:'));
        $output = curl_exec($curld);
        $pile = json_decode($output,true);
        //
        return $pile;
    }
    //
    private function out($v) {
        echo '&#60;pre>';
        print_r($v);
        echo '&#60;/pre>';
    }
    //
    public function unfollow($u = '', $days = 3) {
        $user = SessionControl::getInstance();
        if ($u == '' &#038;&#038; $days > 1) {
            $sql = "SELECT * FROM twitter_users WHERE date &#60; ".$user->quote($this->getSQLDate(time() - ($days * 24 * 60 * 60)),'text');
            $uf = $user->query($sql)->fetchAll();
            foreach ($uf as $u) {
                $details = $this->friendshipDetails($this->username,$u->name);
                //$this->out($details);
                //exit();
                if (isset($details['relationship']['target']['following'])) {
                    $key = $details['relationship']['target']['following'];
                } else {
                    $key = 0;
                }
                // not following, remove
                if ($key != 1) {
                    $this->destroy($u->name);
                    $this->out('Removed ' . $u->name);
                }
                //exit();
                // either way remove from db
                $sqlDel = "DELETE FROM twitter_users WHERE id = $u->id";
                $user->query($sqlDel);
            }
        }
    }
    //
    public function getSQLDate($t) {
        return date('Y-m-d H:i:s',$t);
    }
    //
    public function getUser($u) {
        $url = "http://twitter.com/users/show/$u.json";
        $curld = $this->getCURL();
        curl_setopt($curld, CURLOPT_GET, true);
        curl_setopt($curld, CURLOPT_URL, $url);
        curl_setopt($curld, CURLOPT_HEADER, false);
        curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curld, CURLOPT_HTTPHEADER, array('Expect:'));
        $output = curl_exec($curld);
        $pile = json_decode($output,true);
        return $pile;
    }
    //
    public function getSearch($s) {
        $url = "http://search.twitter.com/search.json?q=$s";
        $curld = $this->getCURL();
        curl_setopt($curld, CURLOPT_GET, true);
        curl_setopt($curld, CURLOPT_URL, $url);
        curl_setopt($curld, CURLOPT_HEADER, false);
        curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curld, CURLOPT_HTTPHEADER, array('Expect:'));
        $output = curl_exec($curld);
        $pile = json_decode($output,true);
        return $pile;
    }
    //
    public function getLimit() {
        $url = "http://twitter.com/account/rate_limit_status.json";
        $curld = $this->getCURL();
        curl_setopt($curld, CURLOPT_GET, true);
        curl_setopt($curld, CURLOPT_URL, $url);
        curl_setopt($curld, CURLOPT_HEADER, false);
        curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curld, CURLOPT_HTTPHEADER, array('Expect:'));
        $output = curl_exec($curld);
        $pile = json_decode($output,true);
        return $pile;

    }
    //
    public function __sleep() {
        return array_keys(get_object_vars($this));
    }
    //
    public function __wakeup() {

    }
}

?></pre>
</div>
<p></code></div>
</div>
<p>From here, the main control file called go.php runs everything.  This is were the steps are executed.  Here it is:</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>&#60;?php

require_once 'twitter.class.php';

set_time_limit(-1);

$twitter = new Twitter();
$limit = $twitter->getLimit();

function getSearchTerm() {
    //$terms = array("twitter_search_term_1",);
    //$r = mt_rand(0,2);
    //return $terms[$r];
    return 'twitter_search_term';
}

if ($limit['remaining_hits'] > 0){
    $me = $twitter->getUser($twitter->getUsername());
    if (($me['followers_count']) > $me['friends_count']) {
        $sterm = getSearchTerm();
        dump('Hits Left: '.$limit['remaining_hits'].' -- Searching: ' . $sterm);
        $search = $twitter->getSearch($sterm);
        foreach ($search['results'] as $r) {
            $user = $r['from_user'];
            if (!$twitter->isUserInDB($user)) {
                $info = $twitter->getUser($user);
                $friends_count = $info['friends_count'];
                $followers_count = $info['followers_count'];
                if ($friends_count > 0 &#038;&#038; $followers_count > 0) {
                    $ratio = (($followers_count - $friends_count) / $followers_count);
                    if ($ratio &#60; 0.2 &#038;&#038; $ratio > -0.4) {
                        $twitter->follow($user);
                        dump($user . ' followed -> friends: ' . $friends_count . ' - Followers: ' . $followers_count);
                    }
                }
            } else {
                dump($user . ' in db');
            }
        }
    }
    //
    $twitter->unfollow();
} else {
    dump($limit);
}

function dump($r) {
    echo '&#60;pre>';
    print_r($r);
    echo '&#60;/pre>';
}
?></pre>
</div>
<p></code></div>
</div>
<p><strong>Key points</strong></p>
<ul>
<li>Twitter has a limit on the number of API calls you can execute based on a user name and IP address.  So don&#8217;t run this a) too often and b) on your personal computer.  You&#8217;ll need to run this on a separate box with a separate IP address.</li>
<li>You&#8217;ll need to automate this with a cron job.  I run it every 10 minutes.  If your hosting company doesn&#8217;t allow for cron jobs, you can use <a href="http://www.webbasedcron.com">WebBasedCron</a>, which I made 4-5 years ago.</li>
<li>The code is released under GPL.  Use at your own risk.  You can download the <a href="http://sematopia.com/upload/twitter-follow.rar">files here</a>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.sematopia.com/2010/04/twitter-how-to-auto-gain-followers-influence-others-no-one/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP: Removing All Non-Printable (Special Characters) From String</title>
		<link>http://www.sematopia.com/2010/02/php-removing-all-non-printable-special-characters-from-string/</link>
		<comments>http://www.sematopia.com/2010/02/php-removing-all-non-printable-special-characters-from-string/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 21:24:30 +0000</pubDate>
		<dc:creator>George A. Papayiannis</dc:creator>
				<category><![CDATA[PHP/MySQL]]></category>

		<guid isPermaLink="false">http://www.sematopia.com/?p=557</guid>
		<description><![CDATA[I was actually trying to find a way to do this in JavaScript, but eventually gave up. I basically want to remove all non-printable special characters from a string. So for a string like this: Code: $str = 'Characters like© and ® are not all®wed.'; I would want &#8216;Characters like and are not allwed.&#8217;. The [...]]]></description>
			<content:encoded><![CDATA[<p>I was actually trying to find a way to do this in JavaScript, but eventually gave up.  I basically want to remove all non-printable special characters from a string.  So for a string like this:</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>$str = 'Characters like© and ® are not all®wed.';</pre>
</div>
<p></code></div>
</div>
<p>I would want &#8216;Characters like and are not allwed.&#8217;.  The following matches anything in the ASCII range of 0-31 &#038; 128-255 and removes it.</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>$str = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $str); </pre>
</div>
<p></code></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.sematopia.com/2010/02/php-removing-all-non-printable-special-characters-from-string/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Apache: Disabling ETags to Improve Performance</title>
		<link>http://www.sematopia.com/2010/01/apache-disabling-etags-to-improve-performance/</link>
		<comments>http://www.sematopia.com/2010/01/apache-disabling-etags-to-improve-performance/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 02:13:12 +0000</pubDate>
		<dc:creator>George A. Papayiannis</dc:creator>
				<category><![CDATA[PHP/MySQL]]></category>

		<guid isPermaLink="false">http://www.sematopia.com/?p=545</guid>
		<description><![CDATA[By removing the ETag header, you disable caches and browsers from being able to validate files, so they are forced to rely on your Cache-Control and Expires header. Basically you can remove If-Modified-Since and If-None-Match requests and their 304 Not Modified Responses. Entity tags (ETags) are a mechanism web servers and the browser use to [...]]]></description>
			<content:encoded><![CDATA[<p>By removing the ETag header, you disable caches and browsers from being able to validate files, so they are forced to rely on your Cache-Control and Expires header. Basically you can remove If-Modified-Since and If-None-Match requests and their 304 Not Modified Responses.</p>
<blockquote><p>
Entity tags (ETags) are a mechanism web servers and the browser use to determine whether a component in the browser&#8217;s cache matches one on the origin server. Since ETags are typically constructed using attributes that make them unique to a specific server hosting a site, the tags will not match when a browser gets the original component from one server and later tries to validate that component on a different server.
</p></blockquote>
<p>Doing this is simple.  First step make sure the Headers mod is enabled:</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>a2enmod headers</pre>
</div>
<p></code></div>
</div>
<p>Then, within your apache2.conf file, add the following:</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>Header unset ETag
FileETag None</pre>
</div>
<p></code></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.sematopia.com/2010/01/apache-disabling-etags-to-improve-performance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Remove New Lines From a String</title>
		<link>http://www.sematopia.com/2010/01/php-remove-new-lines-from-a-string/</link>
		<comments>http://www.sematopia.com/2010/01/php-remove-new-lines-from-a-string/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 20:06:15 +0000</pubDate>
		<dc:creator>George A. Papayiannis</dc:creator>
				<category><![CDATA[PHP/MySQL]]></category>

		<guid isPermaLink="false">http://www.sematopia.com/?p=543</guid>
		<description><![CDATA[You would think a function to remove new lines from a string would exist, but surprisingly it doesn&#8217;t. My first thought was to use the nl2br function. nl2br &#8220;inserts HTML line breaks before all newlines in a string&#8221; and to then strip the br&#8217;s out. So something like this: Code: $string = strip_tags(nl2br($string)); The key [...]]]></description>
			<content:encoded><![CDATA[<p>You would think a function to remove new lines from a string would exist, but surprisingly it doesn&#8217;t.  My first thought was to use the nl2br function.  nl2br &#8220;inserts HTML line breaks before all newlines in a string&#8221; and to then strip the br&#8217;s out.  So something like this:</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>$string = strip_tags(nl2br($string));</pre>
</div>
<p></code></div>
</div>
<p>The key word above is &#8220;before&#8221; though.  The function preserves the new lines and just adds a br after it.  So I decided to write a function to do it for me:</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>function removeNewLines($string) {

    $string = str_replace( "\t", ' ', $string );
    $string = str_replace( "\n", ' ', $string );
    $string = str_replace( "\r", ' ', $string );
    $string = str_replace( "\0", ' ', $string );
    $string = str_replace( "\x0B", ' ', $string );

    return $string;

}</pre>
</div>
<p></code></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.sematopia.com/2010/01/php-remove-new-lines-from-a-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XAMPP: Setting Up Virtual Hosts Using Apache Friends</title>
		<link>http://www.sematopia.com/2009/10/xampp-setting-up-virtual-hosts-using-apache-friends/</link>
		<comments>http://www.sematopia.com/2009/10/xampp-setting-up-virtual-hosts-using-apache-friends/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 19:44:19 +0000</pubDate>
		<dc:creator>George A. Papayiannis</dc:creator>
				<category><![CDATA[PHP/MySQL]]></category>

		<guid isPermaLink="false">http://www.sematopia.com/?p=495</guid>
		<description><![CDATA[This is something I do often for development, but never actually knew if I was doing it correctly. I think I&#8217;ve finally gotten this right. What I want is: To be able to setup VirtualHosts in XAMPP and also define a PHP include path within that. I&#8217;m assuming you generally have an idea how to [...]]]></description>
			<content:encoded><![CDATA[<p>This is something I do often for development, but never actually knew if I was doing it correctly.  I think I&#8217;ve finally gotten this right.  What I want is: To be able to setup VirtualHosts in XAMPP and also define a PHP include path within that.  I&#8217;m assuming you generally have an idea how to setup a VirtualHost in Apache.</p>
<p>The first step is to modify your Hosts file to point traffic to your local computer.  My hosts file is here C:\WINDOWS\system32\drivers\etc\hosts, you&#8217;ll have to search for yours.</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>127.0.0.1		www.sun.com
127.0.0.1		sun.com</pre>
</div>
<p></code></div>
</div>
<p>The next step is to define the VirtualHost within the httpd-vhosts.conf file in the conf directory of your XAMPP setup.</p>
<div class='code_parent'>
<div class='code_title'>Code:</div>
<div class='code_child'><code>
<div class='pre_container'>
<pre>NameVirtualHost *
&#60;VirtualHost *>
	DocumentRoot "C:\xampp\htdocs"
	ServerName localhost
&#60;/VirtualHost>
&#60;VirtualHost *>
	DocumentRoot "C:\xampp\htdocs\sun"
	ServerName www.sun.com
	ServerAlias sun.com
        ErrorDocument 404 /error.php
	&#60;Directory "C:\xampp\htdocs\sun">
		Order allow,deny
		Allow from all
	&#60;/Directory>
	&#60;Directory "C:\xampp\htdocs\sun">
                 php_value include_path ".;C:\php\pear;C:\htdocs\sun\xx"
	&#60;/Directory>
&#60;/VirtualHost></pre>
</div>
<p></code></div>
</div>
<p>Using a php_value directive you can define the include path for that VirtualHost right from conf file &#8211; thank God.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sematopia.com/2009/10/xampp-setting-up-virtual-hosts-using-apache-friends/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

