Skip to content

Instantly share code, notes, and snippets.

View shyamsalimkumar's full-sized avatar

Shyam S Kumar shyamsalimkumar

  • Berlin
  • 12:23 (UTC +02:00)
View GitHub Profile
@shyamsalimkumar
shyamsalimkumar / gist:5752558
Last active December 18, 2015 08:18
PHP - Format currency
function formatCurrencySign($price, $currency, $separator = "")
{
if (!in_array($currency, array('USD', 'GBP', 'EUR')))
{
return $price . $separator . $currency;
}
switch ($currency)
{
case 'USD':
$format = "$" . $separator . $price;
@shyamsalimkumar
shyamsalimkumar / gist:5752547
Last active December 18, 2015 08:18
PHP - Database connection using PDO
<?php
// you can choose from MySQL and SQLite
define('DB_DRIVER', 'mysql');
// SQLite don't use host parameter
define('DB_HOST', 'localhost');
define('DB_USER', 'login');
define('DB_PASS', 'password');
// In case of SQLite usage, change it to path/to/your.db, for example
define('DB_NAME', 'testdb');
@shyamsalimkumar
shyamsalimkumar / gist:5752537
Last active December 18, 2015 08:18
PHP - Convert URL in text
function convertLinks($text) {
$text = preg_replace('/(((f|ht){1}tps?:\/\/)[-a-zA-Z0-9@:;%_\+.~#?&\/\/=]+)/', '<a href="\\1" target="_blank">\\1</a>', $text);
$text = preg_replace('/([[:space:]()[{}])(www.[-a-zA-Z0-9@:;%_\+.~#?&\/\/=]+)/', '\\1<a href="http://\\2" target="_blank">\\2</a>', $text);
$text = preg_replace('/(([0-9a-zA-Z\.\-\_]+)@([0-9a-zA-Z\.\-\_]+)\.([0-9a-zA-Z\.\-\_]+))/', '<a href="mailto:$1">$1</a>', $text);
return $text;
}
@shyamsalimkumar
shyamsalimkumar / gist:5752508
Last active March 2, 2022 05:12
PHP - Whois query
function whois_query($domain) {
// fix the domain name:
$domain = strtolower(trim($domain));
$domain = preg_replace('/^http:\/\//i', '', $domain);
$domain = preg_replace('/^www\./i', '', $domain);
$domain = explode('/', $domain);
$domain = trim($domain[0]);
// split the TLD from domain name
@shyamsalimkumar
shyamsalimkumar / gist:5752485
Last active December 18, 2015 08:09
PHP - Get info about your memory usage
echo "Initial: ".memory_get_usage()." bytes \n";
/* prints
Initial: 361400 bytes
*/
// let's use up some memory
for ($i = 0; $i < 100000; $i++) {
$array []= md5($i);
}
@shyamsalimkumar
shyamsalimkumar / gist:5752477
Last active December 18, 2015 08:09
PHP - Determine the dominant color of an image
$i = imagecreatefromjpeg("image.jpg");
for ($x=0;$x<imagesx($i);$x++) {
for ($y=0;$y<imagesy($i);$y++) {
$rgb = imagecolorat($i,$x,$y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> & 0xFF;
$b = $rgb & 0xFF;
$rTotal += $r;
@shyamsalimkumar
shyamsalimkumar / gist:5752467
Last active December 18, 2015 08:09
PHP - Display source code of any webpage
$lines = file('http://google.com/');
foreach ($lines as $line_num => $line) {
// loop thru each line and prepend line numbers
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "
\n";
}
@shyamsalimkumar
shyamsalimkumar / gist:5752455
Last active December 18, 2015 08:09
PHP - Detect location by IP
function detect_city($ip) {
$default = 'UNKNOWN';
if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost')
$ip = '8.8.8.8';
$curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';
$url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip);
@shyamsalimkumar
shyamsalimkumar / gist:5752412
Last active December 18, 2015 08:09
PHP - Add (th, st, nd, rd, th) to the end of a number
function ordinal($cdnl){
$test_c = abs($cdnl) % 10;
$ext = ((abs($cdnl) %100 < 21 && abs($cdnl) %100 > 4) ? 'th'
: (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1)
? 'th' : 'st' : 'nd' : 'rd' : 'th'));
return $cdnl.$ext;
}
@shyamsalimkumar
shyamsalimkumar / gist:5752403
Last active December 18, 2015 08:09
PHP - Detect browser language
function get_client_language($availableLanguages, $default='en'){
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($langs as $value){
$choice=substr($value,0,2);
if(in_array($choice, $availableLanguages)){
return $choice;
}
}
}