Put the result of phpinfo() in a file

The phpinfo() function is very useful to get all kinds of information about the server you’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());

Read More »

→ Continue Reading

Convert html source to full text

Today, I’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('@<script[^>]*?>.*?</script>@si', // Strip out javascript
     '@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
     '@<[?]php[^>].*?[?]>@si', //scripts php
     '@<[?][^>].*?[?]>@si', //scripts php
     '@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
     '@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments including CDATA
     );$text = preg_replace($search, '', $document);
     return $text;
}

Read More »

→ Continue Reading

Detect an AJAX request

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']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
    // Request has been made by Ajax.
}

Read More »

→ Continue Reading

Cancel magic quotes without changing PHP settings

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’t (or don’t want to) alter the server PHP configuration, this snippet will cancel magic quotes without changing PHP settings.

if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value) {
        $value = is_array($value) ?
                    array_map('stripslashes_deep', $value) :
                    stripslashes($value);

        return $value;
    }

    $_POST = array_map('stripslashes_deep', $_POST);
 }
→ Continue Reading

parseInt() function in PHP

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('/(\d+)/', $string, $array)) {
		return $array[1];
	} else {
		return 0;
	}
}

Read More »

→ Continue Reading

Get destination url of a tinyUrl

TinyUrl is a web service which allow people to shorten urls. It is very useful, but as it hide destination urls there’s always a potential risk while clicking on it.
Here is a function which will return the destination url for a given tinyUrl.

function untinyUrl($tinyurl){
    if($fp = fsockopen ("tinyurl.com", 80, $errno, $errstr, 30)){
    	if ($fp) {
    		fputs ($fp, "HEAD /$tinyurl HTTP/1.0\r\nHost: tinyurl.com\r\n\r\n");
    		$headers = '';
			while (!feof($fp)) {
				$headers .= fgets ($fp,128);
			}
    		fclose ($fp);
    	}

		$arr1=explode("Location:",$headers);
    	$arr=explode("\n",trim($arr1[1]));
    	echo trim($arr[0]);
    }
}

Read More »

→ Continue Reading

Get current url in PHP

Sometimes you may need to get the current url for using in your script. This is quite easy to do with the following function:

function getCurrentUrl(){
	$domain = $_SERVER['HTTP_HOST'];
	$url = "http://" . $domain . $_SERVER['REQUEST_URI'];
 	return $url;
}

Read More »

→ Continue Reading

PHP and Twitter: Testing friendship between two users

Twitter do not provide any tool to test relationships between two users, you have to use the API. If you want to know if a specific user is following you (or someone else), this snippet will definitely help.

/* makes the request */
function make_request($url) {
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	$result = curl_exec($ch);
	curl_close($ch);
	return $result;
}

/* gets the match */
function get_match($regex,$content) {
	preg_match($regex,$content,$matches);
	return $matches[1];
}

/* persons to test */
$person1 = 'phpsnippets';
$person2 = 'catswhocode';

/* send request to twitter */
$url = 'http://twitter.com/friendships/exists';
$format = 'xml';

/* check */
$persons12 = make_request($url.'.'.$format.'?user_a='.$person1.'&user_b='.$person2);
$result = get_match('/<friends>(.*)<\/friends>/isU',$persons12);
echo $result; // returns "true" or "false"

Read More »

→ Continue Reading

Convert currencies using PHP, Google and cURL

Just a few people know that Google have a built-in currencies calculator. (Just type 100$ in euros in Google search box if you want to test it) This calculator works very well, so what about using PHP and cURL and get this functionnality in your own web app?

function currency($from_Currency,$to_Currency,$amount) {
	$amount = urlencode($amount);
	$from_Currency = urlencode($from_Currency);
	$to_Currency = urlencode($to_Currency);
	$url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
	$ch = curl_init();
	$timeout = 0;
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	$rawdata = curl_exec($ch);
	curl_close($ch);
	$data = explode('"', $rawdata);
	$data = explode(' ', $data['3']);
	$var = $data['0'];
	return round($var,2);
}

Read More »

→ Continue Reading

Calculate VAT using PHP

I don’t really see where is the value in Value Added Tax, but as a freelancer I’m forced to calculate it, or better, ask PHP to calculate it for me.

function getvat($price){
	$vat = 21;
	return $price * ($vat / 100);
}

Read More »

→ Continue Reading