Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

Get Form Element's Value in FCKeditor

FCKeditorAPI.GetInstance('form_element').GetHTML();

Extending Array Prototype

Extending array prototype with sum, minimum, maximum and remove

Array.prototype.sum = function(){
	for (var i=0, sum=0; i < this.length; sum += this[i++]);
	return sum;
}
Array.prototype.max = function(){
	return Math.max.apply({},this)
}
Array.prototype.min = function(){
	return Math.min.apply({},this)
}
Array.prototype.remove=function(s){
	for (i=0; i < this.length; i++){
		if (s == this[i]) this.splice(i, 1);
	}
}

Preloading Images with jQuery

Taken from http://www.mattfarina.com/2007/02/01/preloading_images_with_jquery

jQuery.preloadImages = function() {
	for (var i = 0; i < arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}

Checking If Page Contains a Link In PHP

Sometimes it is necessary to verify that a given page really contains a specific link. This is usually done when checking for a reciprocal link in link exchange scripts and so on.

Several things need to be considered in this situation :

* Only actual links count. A plain-text URL should not be accepted.
* Links inside HTML comments (<!– … –>) are are no good.
* Nofollow’ed links are out as well.

Here’s a PHP function that satisfies these requirements :
(http://w-shadow.com/blog/2007/09/25/checking-if-page-contains-a-link-in-php/)

function contains_link($page_url, $link_url) {
    /* Get the page at page_url */
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $page_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    curl_setopt($ch, CURLOPT_USERAGENT, 
    'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);

    $html = curl_exec($ch);
    curl_close($ch);

    if(!$html) return false; 

    /* Remove HTML comments and their contents */ 
    $html = preg_replace('/<!--.*-->/i', '', $html);

    /* Extract all links */
    $regexp='/(<a[\s]+[^>]*href\s*=\s*[\"\']?)([^\'\" >]+)([\'\"]+[^<>]*>)/i';
    if (!preg_match_all($regexp, $html, $matches, PREG_SET_ORDER)) {
	    return false; /* No links on page */
    };

    /* Check each link */
    foreach($matches as $match){
	    /* Skip links that contain rel=nofollow */	
	    if(preg_match('/rel\s*=\s*[\'\"]?nofollow[\'\"]?/i', $match[0])) continue;
	    /* If URL = backlink_url, we've found the backlink */
	    if ($match[2]==$link_url) return true;
    }

    return false;
}

/* Usage example */

if (contains_link('http://w-shadow.com/','http://w-shadow.com/blog/')) {
	echo 'Reciprocal link found.';
} else {
	echo 'Reciprocal link not found.';
};

Empty a Directory with PHP

// ggarciaa at gmail dot com (04-July-2007 01:57)
// I needed to empty a directory, but keeping it
// so I slightly modified the contribution from
// stefano at takys dot it (28-Dec-2005 11:57)
// A short but powerfull recursive function
// that works also if the dirs contain hidden files
//
// $dir = the target directory
// $DeleteMe = if true delete also $dir, if false leave it alone
//
// SureRemoveDir('EmptyMe', false);
// SureRemoveDir('RemoveMe', true);

<?php
function SureRemoveDir($dir, $DeleteMe) {
    if(!$dh = @opendir($dir)) return;
    while (false !== ($obj = readdir($dh))) {
        if($obj=='.' || $obj=='..') continue;
        if (!@unlink($dir.'/'.$obj)) SureRemoveDir($dir.'/'.$obj, true);
    }

    closedir($dh);
    if ($DeleteMe){
        @rmdir($dir);
    }
}
?>

Array of country list in PHP with Zend Framework

Array of country list in PHP with Zend Framework, ordered by country name

<?php
header('Content-Type: text/html; charset=utf-8');
require_once 'Zend/Locale.php';

$locale = new Zend_Locale('en_US');

$countries = ($locale->getTranslationList('country', 'en'));
asort($countries, SORT_LOCALE_STRING);

echo "<pre>";
print_r($countries);
echo "</pre>";

Array of countries in Turkish

Array of countries in Turkish

$countries = Array
(
    [VI] => ABD Virgin Adaları
    [AF] => Afganistan
    [AX] => Aland Adaları
    [DE] => Almanya
    [US] => Amerika Birleşik Devletleri
    [UM] => Amerika Birleşik Devletleri Küçük Dış Adaları
    [AS] => Amerikan Samoası
    [AD] => Andora
    [AO] => Angola
    [AI] => Anguilla
    [AQ] => Antarktika
    [AG] => Antigua ve Barbuda
    [AR] => Arjantin
    [AL] => Arnavutluk
    [AW] => Aruba
    [QU] => Avrupa Birliği
    [AU] => Avustralya
    [AT] => Avusturya
    [AZ] => Azerbaycan
    [BS] => Bahamalar
    [BH] => Bahreyn
    [BD] => Bangladeş
    [BB] => Barbados
    [EH] => Batı Sahara
    [BZ] => Belize
    [BE] => Belçika
    [BJ] => Benin
    [BM] => Bermuda
    [BY] => Beyaz Rusya
    [BT] => Bhutan
    [ZZ] => Bilinmeyen veya Geçersiz Bölge
    [AE] => Birleşik Arap Emirlikleri
    [GB] => Birleşik Krallık
    [BO] => Bolivya
    [BA] => Bosna Hersek
    [BW] => Botsvana
    [BV] => Bouvet Adası
    [BR] => Brezilya
    [BN] => Brunei
    [BG] => Bulgaristan
    [BF] => Burkina Faso
    [BI] => Burundi
    [CV] => Cape Verde
    [GI] => Cebelitarık
    [DZ] => Cezayir
    [CX] => Christmas Adası
    [DJ] => Cibuti
    [CC] => Cocos Adaları
    [CK] => Cook Adaları
    [TD] => Çad
    [CZ] => Çek Cumhuriyeti
    [CN] => Çin
    [DK] => Danimarka
    [DM] => Dominik
    [DO] => Dominik Cumhuriyeti
    [TL] => Doğu Timor
    [EC] => Ekvator
    [GQ] => Ekvator Ginesi
    [SV] => El Salvador
    [ID] => Endonezya
    [ER] => Eritre
    [AM] => Ermenistan
    [EE] => Estonya
    [ET] => Etiyopya
    [FK] => Falkland Adaları (Malvinalar)
    [FO] => Faroe Adaları
    [MA] => Fas
    [FJ] => Fiji
    [CI] => Fildişi Sahilleri
    [PH] => Filipinler
    [PS] => Filistin Bölgesi
    [FI] => Finlandiya
    [FR] => Fransa
    [GF] => Fransız Guyanası
    [TF] => Fransız Güney Bölgeleri
    [PF] => Fransız Polinezyası
    [GA] => Gabon
    [GM] => Gambia
    [GH] => Gana
    [GN] => Gine
    [GW] => Gine-Bissau
    [GD] => Granada
    [GL] => Grönland
    [GP] => Guadeloupe
    [GU] => Guam
    [GT] => Guatemala
    [GG] => Guernsey
    [GY] => Guyana
    [ZA] => Güney Afrika
    [GS] => Güney Georgia ve Güney Sandwich Adaları
    [KR] => Güney Kore
    [CY] => Güney Kıbrıs Rum Kesimi
    [GE] => Gürcistan
    [HT] => Haiti
    [HM] => Heard Adası ve McDonald Adaları
    [IN] => Hindistan
    [IO] => Hint Okyanusu İngiliz Bölgesi
    [NL] => Hollanda
    [AN] => Hollanda Antilleri
    [HN] => Honduras
    [HK] => Hong Kong SAR - Çin
    [HR] => Hırvatistan
    [IQ] => Irak
    [VG] => İngiliz Virgin Adaları
    [IR] => İran
    [IE] => İrlanda
    [ES] => İspanya
    [IL] => İsrail
    [SE] => İsveç
    [CH] => İsviçre
    [IT] => İtalya
    [IS] => İzlanda
    [JM] => Jamaika
    [JP] => Japonya
    [JE] => Jersey
    [KH] => Kamboçya
    [CM] => Kamerun
    [CA] => Kanada
    [ME] => Karadağ
    [QA] => Katar
    [KY] => Kayman Adaları
    [KZ] => Kazakistan
    [KE] => Kenya
    [KI] => Kiribati
    [CO] => Kolombiya
    [KM] => Komorlar
    [CG] => Kongo
    [CD] => Kongo Demokratik Cumhuriyeti
    [CR] => Kosta Rika
    [KW] => Kuveyt
    [KP] => Kuzey Kore
    [MP] => Kuzey Mariana Adaları
    [CU] => Küba
    [KG] => Kırgızistan
    [LA] => Laos
    [LS] => Lesotho
    [LV] => Letonya
    [LR] => Liberya
    [LY] => Libya
    [LI] => Liechtenstein
    [LT] => Litvanya
    [LB] => Lübnan
    [LU] => Lüksemburg
    [HU] => Macaristan
    [MG] => Madagaskar
    [MO] => Makao S.A.R. Çin
    [MK] => Makedonya
    [MW] => Malavi
    [MV] => Maldivler
    [MY] => Malezya
    [ML] => Mali
    [MT] => Malta
    [IM] => Man Adası
    [MH] => Marshall Adaları
    [MQ] => Martinik
    [MU] => Mauritius
    [YT] => Mayotte
    [MX] => Meksika
    [FM] => Mikronezya Federal Eyaletleri
    [MD] => Moldovya Cumhuriyeti
    [MC] => Monako
    [MS] => Montserrat
    [MR] => Moritanya
    [MZ] => Mozambik
    [MN] => Moğolistan
    [MM] => Myanmar
    [EG] => Mısır
    [NA] => Namibya
    [NR] => Nauru
    [NP] => Nepal
    [NE] => Nijer
    [NG] => Nijerya
    [NI] => Nikaragua
    [NU] => Niue
    [NF] => Norfolk Adası
    [NO] => Norveç
    [CF] => Orta Afrika Cumhuriyeti
    [UZ] => Özbekistan
    [PK] => Pakistan
    [PW] => Palau
    [PA] => Panama
    [PG] => Papua Yeni Gine
    [PY] => Paraguay
    [PE] => Peru
    [PN] => Pitcairn
    [PL] => Polonya
    [PT] => Portekiz
    [PR] => Porto Riko
    [RE] => Reunion
    [RO] => Romanya
    [RW] => Ruanda
    [RU] => Rusya Federasyonu
    [SH] => Saint Helena
    [KN] => Saint Kitts ve Nevis
    [LC] => Saint Lucia
    [PM] => Saint Pierre ve Miquelon
    [VC] => Saint Vincent ve Grenadinler
    [WS] => Samoa
    [SM] => San Marino
    [ST] => Sao Tome ve Principe
    [SN] => Senegal
    [SC] => Seyşeller
    [SL] => Sierra Leone
    [SG] => Singapur
    [SK] => Slovakya
    [SI] => Slovenya
    [SB] => Solomon Adaları
    [SO] => Somali
    [LK] => Sri Lanka
    [SD] => Sudan
    [SR] => Surinam
    [SY] => Suriye
    [SA] => Suudi Arabistan
    [SJ] => Svalbard ve Jan Mayen
    [SZ] => Svaziland
    [RS] => Sırbistan
    [CS] => Sırbistan-Karadağ
    [CL] => Şili
    [TJ] => Tacikistan
    [TZ] => Tanzanya
    [TH] => Tayland
    [TW] => Tayvan
    [TG] => Togo
    [TK] => Tokelau
    [TO] => Tonga
    [TT] => Trinidad ve Tobago
    [TN] => Tunus
    [TC] => Turks ve Caicos Adaları
    [TV] => Tuvalu
    [TR] => Türkiye
    [TM] => Türkmenistan
    [UG] => Uganda
    [UA] => Ukrayna
    [OM] => Umman
    [UY] => Uruguay
    [QO] => Uzak Okyanusya
    [JO] => Ürdün
    [VU] => Vanuatu
    [VA] => Vatikan
    [VE] => Venezuela
    [VN] => Vietnam
    [WF] => Wallis ve Futuna
    [YE] => Yemen
    [NC] => Yeni Kaledonya
    [NZ] => Yeni Zelanda
    [GR] => Yunanistan
    [ZM] => Zambiya
    [ZW] => Zimbabve
)
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS