<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.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:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>PHP Snippets</title>
	
	<link>http://www.phpsnippets.info</link>
	<description>We publish daily PHP snippets</description>
	<lastBuildDate>Mon, 25 Oct 2010 13:24:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/phps" /><feedburner:info uri="phps" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Get how many times a page have been retweeted using PHP</title>
		<link>http://feedproxy.google.com/~r/phps/~3/1nTvlCoWM_E/get-how-many-times-a-page-have-been-retweeted-using-php</link>
		<comments>http://www.phpsnippets.info/get-how-many-times-a-page-have-been-retweeted-using-php#comments</comments>
		<pubDate>Mon, 25 Oct 2010 13:24:38 +0000</pubDate>
		<dc:creator>jbj</dc:creator>
				<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://www.phpsnippets.info/?p=261</guid>
		<description><![CDATA[How many times an url have been retweeted? This is a good question, and the answer is quite easy to get using PHP and the Tweetmeme API. Here is a ready to use function: function tweetCount($url) { $content = file_get_contents(&#34;http://api.tweetmeme.com/url_info?url=&#34;.$url); $element = new SimpleXmlElement($content); $retweets = $element-&#62;story-&#62;url_count; if($retweets){ return $retweets; } else { return 0; [...]]]></description>
			<content:encoded><![CDATA[<p>How many times an url have been retweeted? This is a good question, and the answer is quite easy to get using PHP and the Tweetmeme API. Here is a ready to use function:</p>
<pre class="brush: php">
function tweetCount($url) {
    $content = file_get_contents(&quot;http://api.tweetmeme.com/url_info?url=&quot;.$url);
    $element = new SimpleXmlElement($content);
    $retweets = $element-&gt;story-&gt;url_count;
    if($retweets){
        return $retweets;
    } else {
        return 0;
    }
}
</pre>
<p><span id="more-261"></span></p>
<h2>Usage</h2>
<p>Nothing complicated. Just use echo to display how many times an url have been retweeted:</p>
<pre class="brush: php">
echo tweetCount('http://www.phpsnippets.info');
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.phpsnippets.info/get-how-many-times-a-page-have-been-retweeted-using-php/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.phpsnippets.info/get-how-many-times-a-page-have-been-retweeted-using-php</feedburner:origLink></item>
		<item>
		<title>Use PHP’s Tidy extension to validate your (X)HTML markup</title>
		<link>http://feedproxy.google.com/~r/phps/~3/hUgFncz0f9s/use-php%e2%80%99s-tidy-extension-to-validate-your-xhtml-markup</link>
		<comments>http://www.phpsnippets.info/use-php%e2%80%99s-tidy-extension-to-validate-your-xhtml-markup#comments</comments>
		<pubDate>Wed, 20 Oct 2010 10:46:03 +0000</pubDate>
		<dc:creator>jbj</dc:creator>
				<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://www.phpsnippets.info/?p=249</guid>
		<description><![CDATA[In order to check (x)HTML markup, you can use various tools, including the popular online validator. But what about validating your markup using PHP and its Tidy extension? This is what I&#8217;d like to share with you today. ob_start(); /* Your HTML goes here */ $generated_html = ob_get_clean(); $tidy = tidy_parse_string($generated_html); if (mb_strlen($tidy-&#62;errorBuffer) &#62; 0){ [...]]]></description>
			<content:encoded><![CDATA[<p>In order to check (x)HTML markup, you can use various tools, including the popular <a href="http://validator.w3.org/">online validator</a>. But what about validating your markup using PHP and its Tidy extension? This is what I&#8217;d like to share with you today.</p>
<pre class="brush: php">
ob_start();

/* Your HTML goes here */

$generated_html = ob_get_clean();
$tidy = tidy_parse_string($generated_html);

if (mb_strlen($tidy-&gt;errorBuffer) &gt; 0){
    echo &#39;&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &#39; .
         &#39;&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;&#39;;
    echo &#39;&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&#39;;
    echo &#39;&lt;head&gt;&lt;title&gt;HTML Error Log&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&#39;;

    echo &#39;&lt;h1&gt;Errors in your HTML markup&lt;/h1&gt;&#39;;

    echo &#39;&lt;h2&gt;Generated markup&lt;/h2&gt;&#39;;
    echo &#39;&lt;pre&gt;&#39; . htmlspecialchars($generated_html) . &#39;&lt;/pre&gt;&#39;;

    echo &#39;&lt;h2&gt;Error log&lt;/h2&gt;&#39;;
    echo &#39;&lt;pre&gt;&#39; . htmlspecialchars($tidy-&gt;errorBuffer) . &#39;&lt;/pre&gt;&#39;;

    echo &#39;&lt;/body&gt;&lt;/html&gt;&#39;;
} else {
    // HTML is valid -&gt; print it
    echo $generated_html;
}
</pre>
<p><span id="more-249"></span></p>
<h2>Using the snippet</h2>
<p>Nothing complicated with this code snippet: Paste your html on line 3, save the file and view it throught your web browser. If any markup error has been found by Tidy, they&#8217;ll be displayed on the screen.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpsnippets.info/use-php%e2%80%99s-tidy-extension-to-validate-your-xhtml-markup/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.phpsnippets.info/use-php%e2%80%99s-tidy-extension-to-validate-your-xhtml-markup</feedburner:origLink></item>
		<item>
		<title>Finding difference of days between two dates in PHP</title>
		<link>http://feedproxy.google.com/~r/phps/~3/dbkrMxdaSEo/finding-difference-of-days-between-two-dates-in-php</link>
		<comments>http://www.phpsnippets.info/finding-difference-of-days-between-two-dates-in-php#comments</comments>
		<pubDate>Tue, 12 Oct 2010 14:02:39 +0000</pubDate>
		<dc:creator>jbj</dc:creator>
				<category><![CDATA[Dates]]></category>

		<guid isPermaLink="false">http://www.phpsnippets.info/?p=251</guid>
		<description><![CDATA[Calculating the difference of days between two dates isn&#8217;t always easy, so today I&#8217;d like to share an useful snippet which takes two dates and return the difference of days between them. A bit quick and dirty, but it do the job. function daysDifference($endDate, $beginDate){ $date_parts1=explode(&#34;-&#34;, $beginDate); $date_parts2=explode(&#34;-&#34;, $endDate); $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]); $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]); [...]]]></description>
			<content:encoded><![CDATA[<p>Calculating the difference of days between two dates isn&#8217;t always easy, so today I&#8217;d like to share an useful snippet which takes two dates and return the difference of days between them. A bit <em>quick and dirty</em>, but it do the job.</p>
<pre class="brush: php">
function daysDifference($endDate, $beginDate){
   $date_parts1=explode(&quot;-&quot;, $beginDate);
   $date_parts2=explode(&quot;-&quot;, $endDate);
   $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
   $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
   return $end_date - $start_date;
}
</pre>
<p><span id="more-251"></span></p>
<h2>Usage</h2>
<p>Simply call the function with two dates (format is: YYYY-MM-DD) and it will return the number of days:</p>
<pre class="brush: php">
echo daysDifference('2010-10-12','2010-10-09');
</pre>
<p>This snippet can easily be improved, so don&#8217;t hesitate to share your version in a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpsnippets.info/finding-difference-of-days-between-two-dates-in-php/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.phpsnippets.info/finding-difference-of-days-between-two-dates-in-php</feedburner:origLink></item>
		<item>
		<title>Easily modify url parameters</title>
		<link>http://feedproxy.google.com/~r/phps/~3/cdDpmXAAYds/easily-modify-url-parameters</link>
		<comments>http://www.phpsnippets.info/easily-modify-url-parameters#comments</comments>
		<pubDate>Tue, 05 Oct 2010 21:27:31 +0000</pubDate>
		<dc:creator>jbj</dc:creator>
				<category><![CDATA[Server]]></category>

		<guid isPermaLink="false">http://www.phpsnippets.info/?p=243</guid>
		<description><![CDATA[Today, here is a very handy function to easily modify, replaces or removes the url query. This can typically be used in paging situations where there are more parameters than the page. The function can detect if there&#8217;s currently no query string so it will append &#8216;?&#8217; instead of &#038; to the actual url. &#60;?php [...]]]></description>
			<content:encoded><![CDATA[<p>Today, here is a very handy function to easily modify, replaces or removes the url query. This can typically be used in paging situations where there are more parameters than the page. The function can detect if there&#8217;s currently no query string so it will append &#8216;?&#8217; instead of &#038; to the actual url.</p>
<pre class="brush: php">
&lt;?php
function modify_url($mod){
    $url = &quot;http://&quot;.$_SERVER[&#39;HTTP_HOST&#39;].$_SERVER[&#39;REQUEST_URI&#39;];
    $query = explode(&quot;&amp;&quot;, $_SERVER[&#39;QUERY_STRING&#39;]);
    if (!$_SERVER[&#39;QUERY_STRING&#39;]) {$queryStart = &quot;?&quot;;} else {$queryStart = &quot;&amp;&quot;;}
    // modify/delete data
    foreach($query as $q)
    {
        list($key, $value) = explode(&quot;=&quot;, $q);
        if(array_key_exists($key, $mod))
        {
            if($mod[$key])
            {
                $url = preg_replace(&#39;/&#39;.$key.&#39;=&#39;.$value.&#39;/&#39;, $key.&#39;=&#39;.$mod[$key], $url);
            }
            else
            {
                $url = preg_replace(&#39;/&amp;?&#39;.$key.&#39;=&#39;.$value.&#39;/&#39;, &#39;&#39;, $url);
            }
        }
    }
    // add new data
    foreach($mod as $key =&gt; $value)
    {
        if($value &amp;&amp; !preg_match(&#39;/&#39;.$key.&#39;=/&#39;, $url))
        {
            $url .= $queryStart.$key.&#39;=&#39;.$value;
        }
    }
    return $url;
}
?&gt;
</pre>
<p><span id="more-243"></span></p>
<h2>Usage and example</h2>
<p>If the current url is <em>http://www.example.com/page.php?p=5&amp;show=list&amp;style=2</em>, and you apply the function with the following parameters:</p>
<pre class="brush: php">
$url = modify_url(array(&#39;p&#39; =&gt; 4, &#39;show&#39; =&gt; &#39;column&#39;));
</pre>
<p>Your url will become <em>http://www.example.com/page.php?p=4&amp;show=column&amp;style=2</em> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpsnippets.info/easily-modify-url-parameters/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.phpsnippets.info/easily-modify-url-parameters</feedburner:origLink></item>
		<item>
		<title>Calculate distances in PHP</title>
		<link>http://feedproxy.google.com/~r/phps/~3/xmpFlPp2ZvM/calculate-distances-in-php</link>
		<comments>http://www.phpsnippets.info/calculate-distances-in-php#comments</comments>
		<pubDate>Sat, 25 Sep 2010 07:30:09 +0000</pubDate>
		<dc:creator>jbj</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.phpsnippets.info/?p=237</guid>
		<description><![CDATA[Here is a very handy function, which calculate the distance from a point A to a point B, using latitudes and longitudes. The function can return the distance in miles, kilometers, or nautical miles. function distance($lat1, $lon1, $lat2, $lon2, $unit) { $theta = $lon1 - $lon2; $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a very handy function, which calculate the distance from a point A to a point B, using latitudes and longitudes. The function can return the distance in miles, kilometers, or nautical miles.</p>
<pre class="brush: php">
function distance($lat1, $lon1, $lat2, $lon2, $unit) { 

  $theta = $lon1 - $lon2;
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;
  $unit = strtoupper($unit);

  if ($unit == &quot;K&quot;) {
    return ($miles * 1.609344);
  } else if ($unit == &quot;N&quot;) {
      return ($miles * 0.8684);
    } else {
        return $miles;
      }
}
</pre>
<p><span id="more-237"></span></p>
<h2>Usage</h2>
<p>Using the function is pretty simple: All you need to do is to call it. Parameters are: <em>from</em> latitude, <em>from</em> longitude, <em>to</em> latitude, <em>to</em> longitude and <em>unit</em>, which can be <em>m</em> for miles, <em>k</em> for kilometers, and <em>n</em> for nautical miles.</p>
<pre class="brush: php">
//Miles
echo distance(32.9697, -96.80322, 29.46786, -98.53506, &quot;m&quot;) . &quot; miles&lt;br/&gt;&quot;;

//Kilometers
echo distance(32.9697, -96.80322, 29.46786, -98.53506, &quot;k&quot;) . &quot; kilometers&lt;br/&gt;&quot;;

//Nautical miles
echo distance(32.9697, -96.80322, 29.46786, -98.53506, &quot;n&quot;) . &quot; nautical miles&lt;br/&gt;&quot;;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.phpsnippets.info/calculate-distances-in-php/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.phpsnippets.info/calculate-distances-in-php</feedburner:origLink></item>
		<item>
		<title>Put the result of phpinfo() in a file</title>
		<link>http://feedproxy.google.com/~r/phps/~3/G81jBknoiko/put-the-result-of-phpinfo-in-a-file</link>
		<comments>http://www.phpsnippets.info/put-the-result-of-phpinfo-in-a-file#comments</comments>
		<pubDate>Mon, 09 Aug 2010 11:46:12 +0000</pubDate>
		<dc:creator>jbj</dc:creator>
				<category><![CDATA[Files]]></category>
		<category><![CDATA[Server]]></category>

		<guid isPermaLink="false">http://www.phpsnippets.info/?p=222</guid>
		<description><![CDATA[The phpinfo() function is very useful to get all kinds of information about the server you&#8217;re working on. But what about saving the result of the function in a file instead of displaying it on screen? The following snippet will do that. ob_start(); phpinfo(); file_put_contents('filename.txt', ob_get_clean());]]></description>
			<content:encoded><![CDATA[<p>The phpinfo() function is very useful to get all kinds of information about the server you&#8217;re working on. But what about saving the result of the function in a file  instead of displaying it on screen?<br />
The following snippet will do that.</p>
<pre class="brush: php">
ob_start();
phpinfo();
file_put_contents('filename.txt', ob_get_clean());
</pre>
<p><span id="more-222"></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpsnippets.info/put-the-result-of-phpinfo-in-a-file/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.phpsnippets.info/put-the-result-of-phpinfo-in-a-file</feedburner:origLink></item>
		<item>
		<title>Convert html source to full text</title>
		<link>http://feedproxy.google.com/~r/phps/~3/xeK1CBxWN0A/convert-html-source-to-full-text</link>
		<comments>http://www.phpsnippets.info/convert-html-source-to-full-text#comments</comments>
		<pubDate>Tue, 03 Aug 2010 05:32:48 +0000</pubDate>
		<dc:creator>jbj</dc:creator>
				<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://www.phpsnippets.info/?p=226</guid>
		<description><![CDATA[Today, I&#8217;d like to share a pretty useful function, which turn a html source into a full text document by removing all html tags and other unneeded code. function html2txt($document){ $search = array(&#39;@&#60;script[^&#62;]*?&#62;.*?&#60;/script&#62;@si&#39;, // Strip out javascript &#39;@&#60;style[^&#62;]*?&#62;.*?&#60;/style&#62;@siU&#39;, // Strip style tags properly &#39;@&#60;[?]php[^&#62;].*?[?]&#62;@si&#39;, //scripts php &#39;@&#60;[?][^&#62;].*?[?]&#62;@si&#39;, //scripts php &#39;@&#60;[\/\!]*?[^&#60;&#62;]*?&#62;@si&#39;, // Strip out HTML tags [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I&#8217;d like to share a pretty useful function, which turn a html source into a full text document by removing all html tags and other unneeded code.</p>
<pre class="brush: php">
function html2txt($document){
     $search = array(&#39;@&lt;script[^&gt;]*?&gt;.*?&lt;/script&gt;@si&#39;, // Strip out javascript
     &#39;@&lt;style[^&gt;]*?&gt;.*?&lt;/style&gt;@siU&#39;, // Strip style tags properly
     &#39;@&lt;[?]php[^&gt;].*?[?]&gt;@si&#39;, //scripts php
     &#39;@&lt;[?][^&gt;].*?[?]&gt;@si&#39;, //scripts php
     &#39;@&lt;[\/\!]*?[^&lt;&gt;]*?&gt;@si&#39;, // Strip out HTML tags
     &#39;@&lt;![\s\S]*?--[ \t\n\r]*&gt;@&#39; // Strip multi-line comments including CDATA
     );$text = preg_replace($search, &#39;&#39;, $document);
     return $text;
}
</pre>
<p><span id="more-226"></span></p>
<h2>Usage</h2>
<p>Simply pass a html source as a parameter to the <em>html2txt()</em> function, and it will return a full text file.</p>
<pre class="brush: php">
$html_source = file_get_contents('http://www.phpsnippets.info');
$txt = html2txt($html_source);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.phpsnippets.info/convert-html-source-to-full-text/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.phpsnippets.info/convert-html-source-to-full-text</feedburner:origLink></item>
		<item>
		<title>Detect an AJAX request</title>
		<link>http://feedproxy.google.com/~r/phps/~3/_VDRzPLj30w/detect-an-ajax-request</link>
		<comments>http://www.phpsnippets.info/detect-an-ajax-request#comments</comments>
		<pubDate>Thu, 29 Jul 2010 14:34:42 +0000</pubDate>
		<dc:creator>jbj</dc:creator>
				<category><![CDATA[Server]]></category>

		<guid isPermaLink="false">http://www.phpsnippets.info/?p=67</guid>
		<description><![CDATA[Sometimes you may want to check if a request has been made by Ajax. The following snippet will easily let you know if a request has been made by Ajax or not. if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &#038;&#038; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){ // Request has been made by Ajax. }]]></description>
			<content:encoded><![CDATA[<p>Sometimes you may want to check if a request has been made by Ajax. The following snippet will easily let you know if a request has been made by Ajax or not.</p>
<pre class="brush: php">
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &#038;&#038; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
    // Request has been made by Ajax.
}
</pre>
<p><span id="more-67"></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpsnippets.info/detect-an-ajax-request/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.phpsnippets.info/detect-an-ajax-request</feedburner:origLink></item>
		<item>
		<title>Cancel magic quotes without changing PHP settings</title>
		<link>http://feedproxy.google.com/~r/phps/~3/lrggpwywO74/cancel-magic-quotes-without-changing-php-settings</link>
		<comments>http://www.phpsnippets.info/cancel-magic-quotes-without-changing-php-settings#comments</comments>
		<pubDate>Tue, 27 Jul 2010 09:11:12 +0000</pubDate>
		<dc:creator>jbj</dc:creator>
				<category><![CDATA[Server]]></category>

		<guid isPermaLink="false">http://www.phpsnippets.info/?p=190</guid>
		<description><![CDATA[Although magic quotes has been deprecated as of PHP 5.3.0, this feature, which automatically escapes incoming data to the PHP script, is still used on many servers. If for some reason you can&#8217;t (or don&#8217;t want to) alter the server PHP configuration, this snippet will cancel magic quotes without changing PHP settings. if (get_magic_quotes_gpc()) { [...]]]></description>
			<content:encoded><![CDATA[<p>Although magic quotes has been deprecated as of PHP 5.3.0, this feature, which automatically escapes incoming data to the PHP script, is still used on many servers.<br />
If for some reason you can&#8217;t (or don&#8217;t want to) alter the server PHP configuration, this snippet will cancel magic quotes without changing PHP settings. </p>
<pre class="brush: php">
if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value) {
        $value = is_array($value) ?
                    array_map(&#39;stripslashes_deep&#39;, $value) :
                    stripslashes($value);

        return $value;
    }

    $_POST = array_map(&#39;stripslashes_deep&#39;, $_POST);
 }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.phpsnippets.info/cancel-magic-quotes-without-changing-php-settings/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.phpsnippets.info/cancel-magic-quotes-without-changing-php-settings</feedburner:origLink></item>
		<item>
		<title>parseInt() function in PHP</title>
		<link>http://feedproxy.google.com/~r/phps/~3/CH2KWnm8Jz4/parseint-function-php</link>
		<comments>http://www.phpsnippets.info/parseint-function-php#comments</comments>
		<pubDate>Fri, 23 Jul 2010 18:15:05 +0000</pubDate>
		<dc:creator>jbj</dc:creator>
				<category><![CDATA[Numbers]]></category>

		<guid isPermaLink="false">http://www.phpsnippets.info/?p=193</guid>
		<description><![CDATA[Javascript have a useful parseInt() function, which convert a variable in an integer. The following snippet is this function, ported to PHP. function parseInt($string) { // return intval($string); if(preg_match(&#39;/(\d+)/&#39;, $string, $array)) { return $array[1]; } else { return 0; } } Usage Using the function is definitely easy. Just pass a variable as a parameter [...]]]></description>
			<content:encoded><![CDATA[<p>Javascript have a useful parseInt() function, which convert a variable in an integer. The following snippet is this function, ported to PHP.</p>
<pre class="brush: php">
function parseInt($string) {
//	return intval($string);
	if(preg_match(&#39;/(\d+)/&#39;, $string, $array)) {
		return $array[1];
	} else {
		return 0;
	}
}
</pre>
<p><span id="more-193"></span></p>
<h2>Usage</h2>
<p>Using the function is definitely easy. Just pass a variable as a parameter and the function will return an integer, as shown in the example below:</p>
<pre class="brush: php">
echo parseInt("2008"); // Result: 2008
echo parseInt("99.90 dollars"); // Result: 99
echo parseInt("www.w3.org"); // Result: 3
echo parseInt("300 spartiates"); // Result: 300
echo parseInt("block text..."); // Result: 0
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.phpsnippets.info/parseint-function-php/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.phpsnippets.info/parseint-function-php</feedburner:origLink></item>
	</channel>
</rss>

