©
. Document créé le 1er janvier 2001 , mis à jour le 9 octobre 2011.C'est le devoir de chaque homme de rendre au monde au moins autant qu'il en a reçu. Albert Einstein
Accueil du site > PHP Lib > PHP Lib IS
Fonctions PHP IS : fonctions de comparaison et de validation de variables.
Confirme s’il s’agit d’un TLD pays (country TLD)
Confirme s’il s’agit d’un TLD générique (generic TLD)
Valide un code postal français
Contrôle d’une adresse e-mail
Valide une adresse IP (Internet Protocol)
Si une chaine ne contient que des nombres
Demande si une chaine de caractères contient un espace
Valide un code ZIP (Zone Improvement Plan)
Une chaîne de caractères en UTF-8
Une fonction IS renvoie en général un TRUE pour confirmer la valeur proposée, ou un FALSE dans le cas inverse. Quoi de plus simple ?
Confirme s’il s’agit d’un TLD pays (country TLD).
<?php
// kzo_is_cctld.inc.php
// chris@quesaco.org - ven. 08/06/2002
/**
* kzo_is_cctld(), confirms whether string is TLD country code.
*
* @author <a href="mailto:chris@quesaco.org">Christian Paulus</a>
* @version 1.1, 20080515
* @param $code target code
* @return Country name or FALSE
* @see <a href="http://www.iana.org/cctld/cctld-whois.htm">Iana Country Code TLD</a>
* @since PHP3
*/
$countryCodeTLD = array( // 252 countries @ Thu May 15 09:01:43 CEST 2008
'ac' => "Ascension Island"
, 'ad' => "Andorra"
, 'ae' => "United Arab Emirates"
, 'af' => "Afghanistan"
, 'ag' => "Antigua and Barbuda"
, 'ai' => "Anguilla"
, 'al' => "Albania"
, 'am' => "Armenia"
, 'an' => "Netherlands Antilles"
, 'ao' => "Angola"
, 'aq' => "Antarctica"
, 'ar' => "Argentina"
, 'as' => "American Samoa"
, 'at' => "Austria"
, 'au' => "Australia"
, 'aw' => "Aruba"
, 'ax' => "Aland Islands"
, 'az' => "Azerbaijan"
, 'ba' => "Bosnia and Herzegovina"
, 'bb' => "Barbados"
, 'bd' => "Bangladesh"
, 'be' => "Belgium"
, 'bf' => "Burkina Faso"
, 'bg' => "Bulgaria"
, 'bh' => "Bahrain"
, 'bi' => "Burundi"
, 'bj' => "Benin"
, 'bl' => "Saint Barthelemy"
, 'bm' => "Bermuda"
, 'bn' => "Brunei Darussalam"
, 'bo' => "Bolivia"
, 'br' => "Brazil"
, 'bs' => "Bahamas"
, 'bt' => "Bhutan"
, 'bv' => "Bouvet Island"
, 'bw' => "Botswana"
, 'by' => "Belarus"
, 'bz' => "Belize"
, 'ca' => "Canada"
, 'cc' => "Cocos (Keeling) Islands"
, 'cd' => "Congo, The Democratic Republic of the"
, 'cf' => "Central African Republic"
, 'cg' => "Congo"
, 'ch' => "Switzerland"
, 'ci' => "Cote d'Ivoire"
, 'ck' => "Cook Islands"
, 'cl' => "Chile"
, 'cm' => "Cameroon"
, 'cn' => "China"
, 'co' => "Colombia"
, 'cr' => "Costa Rica"
, 'cu' => "Cuba"
, 'cv' => "Cape Verde"
, 'cx' => "Christmas Island"
, 'cy' => "Cyprus"
, 'cz' => "Czech Republic"
, 'de' => "Germany"
, 'dj' => "Djibouti"
, 'dk' => "Denmark"
, 'dm' => "Dominica"
, 'do' => "Dominican Republic"
, 'dz' => "Algeria"
, 'ec' => "Ecuador"
, 'ee' => "Estonia"
, 'eg' => "Egypt"
, 'eh' => "Western Sahara"
, 'er' => "Eritrea"
, 'es' => "Spain"
, 'et' => "Ethiopia"
, 'eu' => "European Union"
, 'fi' => "Finland"
, 'fj' => "Fiji"
, 'fk' => "Falkland Islands (Malvinas)"
, 'fm' => "Micronesia, Federated States of"
, 'fo' => "Faroe Islands"
, 'fr' => "France"
, 'ga' => "Gabon"
, 'gb' => "United Kingdom"
, 'gd' => "Grenada"
, 'ge' => "Georgia"
, 'gf' => "French Guiana"
, 'gg' => "Guernsey"
, 'gh' => "Ghana"
, 'gi' => "Gibraltar"
, 'gl' => "Greenland"
, 'gm' => "Gambia"
, 'gn' => "Guinea"
, 'gp' => "Guadeloupe"
, 'gq' => "Equatorial Guinea"
, 'gr' => "Greece"
, 'gs' => "South Georgia and the South Sandwich Islands"
, 'gt' => "Guatemala"
, 'gu' => "Guam"
, 'gw' => "Guinea-Bissau"
, 'gy' => "Guyana"
, 'hk' => "Hong Kong"
, 'hm' => "Heard Island and McDonald Islands"
, 'hn' => "Honduras"
, 'hr' => "Croatia"
, 'ht' => "Haiti"
, 'hu' => "Hungary"
, 'id' => "Indonesia"
, 'ie' => "Ireland"
, 'il' => "Israel"
, 'im' => "Isle of Man"
, 'in' => "India"
, 'io' => "British Indian Ocean Territory"
, 'iq' => "Iraq"
, 'ir' => "Iran, Islamic Republic of"
, 'is' => "Iceland"
, 'it' => "Italy"
, 'je' => "Jersey"
, 'jm' => "Jamaica"
, 'jo' => "Jordan"
, 'jp' => "Japan"
, 'ke' => "Kenya"
, 'kg' => "Kyrgyzstan"
, 'kh' => "Cambodia"
, 'ki' => "Kiribati"
, 'km' => "Comoros"
, 'kn' => "Saint Kitts and Nevis"
, 'kp' => "Korea, Democratic People's Republic of"
, 'kr' => "Korea, Republic of"
, 'kw' => "Kuwait"
, 'ky' => "Cayman Islands"
, 'kz' => "Kazakhstan"
, 'la' => "Lao People's Democratic Republic"
, 'lb' => "Lebanon"
, 'lc' => "Saint Lucia"
, 'li' => "Liechtenstein"
, 'lk' => "Sri Lanka"
, 'lr' => "Liberia"
, 'ls' => "Lesotho"
, 'lt' => "Lithuania"
, 'lu' => "Luxembourg"
, 'lv' => "Latvia"
, 'ly' => "Libyan Arab Jamahiriya"
, 'ma' => "Morocco"
, 'mc' => "Monaco"
, 'md' => "Moldova, Republic of"
, 'me' => "Montenegro"
, 'mf' => "Saint Martin (French part)"
, 'mg' => "Madagascar"
, 'mh' => "Marshall Islands"
, 'mk' => "Macedonia, The Former Yugoslav Republic of"
, 'ml' => "Mali"
, 'mm' => "Myanmar"
, 'mn' => "Mongolia"
, 'mo' => "Macao"
, 'mp' => "Northern Mariana Islands"
, 'mq' => "Martinique"
, 'mr' => "Mauritania"
, 'ms' => "Montserrat"
, 'mt' => "Malta"
, 'mu' => "Mauritius"
, 'mv' => "Maldives"
, 'mw' => "Malawi"
, 'mx' => "Mexico"
, 'my' => "Malaysia"
, 'mz' => "Mozambique"
, 'na' => "Namibia"
, 'nc' => "New Caledonia"
, 'ne' => "Niger"
, 'nf' => "Norfolk Island"
, 'ng' => "Nigeria"
, 'ni' => "Nicaragua"
, 'nl' => "Netherlands"
, 'no' => "Norway"
, 'np' => "Nepal"
, 'nr' => "Nauru"
, 'nu' => "Niue"
, 'nz' => "New Zealand"
, 'om' => "Oman"
, 'pa' => "Panama"
, 'pe' => "Peru"
, 'pf' => "French Polynesia"
, 'pg' => "Papua New Guinea"
, 'ph' => "Philippines"
, 'pk' => "Pakistan"
, 'pl' => "Poland"
, 'pm' => "Saint Pierre and Miquelon"
, 'pn' => "Pitcairn"
, 'pr' => "Puerto Rico"
, 'ps' => "Palestinian Territory, Occupied"
, 'pt' => "Portugal"
, 'pw' => "Palau"
, 'py' => "Paraguay"
, 'qa' => "Qatar"
, 're' => "Reunion"
, 'ro' => "Romania"
, 'rs' => "Serbia"
, 'ru' => "Russian Federation"
, 'rw' => "Rwanda"
, 'sa' => "Saudi Arabia"
, 'sb' => "Solomon Islands"
, 'sc' => "Seychelles"
, 'sd' => "Sudan"
, 'se' => "Sweden"
, 'sg' => "Singapore"
, 'sh' => "Saint Helena"
, 'si' => "Slovenia"
, 'sj' => "Svalbard and Jan Mayen"
, 'sk' => "Slovakia"
, 'sl' => "Sierra Leone"
, 'sm' => "San Marino"
, 'sn' => "Senegal"
, 'so' => "Somalia"
, 'sr' => "Suriname"
, 'st' => "Sao Tome and Principe"
, 'su' => "Soviet Union (being phased out)"
, 'sv' => "El Salvador"
, 'sy' => "Syrian Arab Republic"
, 'sz' => "Swaziland"
, 'tc' => "Turks and Caicos Islands"
, 'td' => "Chad"
, 'tf' => "French Southern Territories"
, 'tg' => "Togo"
, 'th' => "Thailand"
, 'tj' => "Tajikistan"
, 'tk' => "Tokelau"
, 'tl' => "Timor-Leste"
, 'tm' => "Turkmenistan"
, 'tn' => "Tunisia"
, 'to' => "Tonga"
, 'tp' => "Portuguese Timor (being phased out)"
, 'tr' => "Turkey"
, 'tt' => "Trinidad and Tobago"
, 'tv' => "Tuvalu"
, 'tw' => "Taiwan"
, 'tz' => "Tanzania, United Republic of"
, 'ua' => "Ukraine"
, 'ug' => "Uganda"
, 'uk' => "United Kingdom"
, 'um' => "United States Minor Outlying Islands"
, 'us' => "United States"
, 'uy' => "Uruguay"
, 'uz' => "Uzbekistan"
, 'va' => "Holy See (Vatican City State)"
, 'vc' => "Saint Vincent and the Grenadines"
, 've' => "Venezuela"
, 'vg' => "Virgin Islands, British"
, 'vi' => "Virgin Islands, U.S."
, 'vn' => "Viet Nam"
, 'vu' => "Vanuatu"
, 'wf' => "Wallis and Futuna"
, 'ws' => "Samoa"
, 'ye' => "Yemen"
, 'yt' => "Mayotte"
, 'yu' => "Yugoslavia"
, 'za' => "South Africa"
, 'zm' => "Zambia"
, 'zw' => "Zimbabwe"
);
function kzo_is_cctld ($code="") {
global $countryCodeTLD;
if(is_string($code)) {
$code = strtolower(trim($code));
if(!empty($code) && isset($countryCodeTLD[$code])) {
return($countryCodeTLD[$code]);
}
}
return(false);
}
//$test="fr"; echo ($test.": ".kzo_is_cctld($test));
?>
Confirme s’il s’agit d’un TLD générique (generic TLD).
<?php
// kzo_is_gtld.inc.php
// chris@quesaco.org - ven. 08/06/2002
/**
* kzo_is_gtld(), confirms if $code is generic TLD
*
* @author <a href="mailto:chris@quesaco.org">Christian Paulus</a>
* @version 1.1, 20080515
* @param $code target code
* @return Generic name or organization or FALSE.
* @see <a href="http://www.iana.org/domains/root/db/">Iana Generic TLD</a>
* @since PHP3
*/
$genericTLD = array (
// 21 gtld @ Thu May 15 09:01:43 CEST 2008
'aero' => "Air-transport industry"
,'arpa' => "Internet Architecture Board"
,'asia' => "Pan-Asia and Asia Pacific community"
,'biz' => "Business"
,'cat' => "Catalan linguistic and cultural community"
,'com' => "Generic top-level domain"
,'coop' => "Cooperative associations"
,'edu' => "Department of Education's list"
,'gov' => "United States Government"
,'info' => "Generic top-level domain"
,'int' => "International treaties between governments"
,'jobs' => "Human resource managers"
,'mil' => "United States Military"
,'mobi' => "Mobile products and services"
,'museum' => "Museums"
,'name' => "Individuals"
,'net' => "Generic top-level domain"
,'org' => "Generic top-level domain"
,'pro' => "Professionals and related entities"
,'tel' => "Businesses and individuals contact data"
,'travel' => "Travel industry"
);
function kzo_is_gtld ($code="") {
global $genericTLD;
if (is_string($code)) {
$code = strtolower(trim($code));
if (!empty($code) && isset($genericTLD[$code])) {
return($genericTLD[$code]);
}
}
return(false);
}
//$test="aero"; echo ($test.": ".kzo_is_gtld($test));
?>
Valide un code postal français.
Contrôle d’une adresse e-mail.
<?php
// kzo_is_emailaddress.inc.php
// chris@quesaco.org - jeu. 06/06/2002
/*
* Une adresse email est composée d'un nom account et d'un domain séparés par @
* - account est composé d'au moins un caractère
* - account peut contenir des "." "-" "_" et des caratères alpha-numériques
* - domain doit contenir au moins un "."
* - domain peut contenir "-" et des caratères alpha-numériques, mais pas de "_"
* - domain ne doit pas contenir "..". Si c'est le cas, remplace par "."
* - domain ne peut pas être un TLD (doit donc avoir un sous-domain)
* - le TLD est au moins de deux caractères
* - domain doit commencer et finir par un alpha-numérique
*/
function kzo_is_emailaddress ($address) {
//
$address = trim ($address);
//
if (strlen ($address)>4 && ereg ("@", $address)) {
list ($account, $domain) = split ("@", $address);
$account = trim ($account);
$domain = trim ($domain);
// supprime les points répétitifs éventuels
$domain = ereg_replace ("\.+", ".", $domain);
// retire les espaces (erreur de saisie)
$domain = ereg_replace ("[[:space:]]", "", $domain);
// supprime les caractères de bords non alpha-numériques
$domain = ereg_replace ("^([^[:alnum:]]*)", "", $domain);
$domain = ereg_replace ("([^[:alnum:]]*)$", "", $domain);
//
if (
strlen ($account)
&& strlen ($domain)
&& ereg ("^[[:alnum:]_\.-]+$", $account)
&& ereg ("[[:alnum:]-]+\.[[:alnum:]]{2,}$", $domain)
) {
return ($account."@".$domain);
}
}
return (FALSE);
}
?>
Valide une adresse IP (Internet Protocol)
<?php
/*
* Une adresse IP(v4) est composée de 4 nombres séparés par un point "."
* Chacun de ces nombres est >=0 et <=255
*
* Nota: contrôler $ipaddress avant envoi. Ce doit être une string.
*/
function kzo_is_ipaddress ($ipaddress="") {
if (ereg ("^[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}$", $ipaddress)) {
$parts = explode(".", $ipaddress);
while (list($key, $val) = each( $parts )) {
if ($val<0 || $val>255) {
return (FALSE);
}
}
return (TRUE);
}
return (FALSE);
}
?>
Si une chaine ne contient que des nombres ?
Demande si une chaine de caractères contient un espace.
Valide un code ZIP (Zone Improvement Plan)
<?
// kzo_is_zipcode.inc.php
// chris@quesaco.org - sam. 08/06/2002
/**
* kzo_is_zipcode(), valid zip (Zone Improvement Plan) code
*
* @author chris at quesaco.org
* @version 1.0, 08/06/2002
* @param $code string to valid
* @return TRUE if OK. FALSE otherwise
* @since PHP3
*/
/*
* Le zip code est composé de 5 chiffres.
* zip+4 est un zip suivi d'un tiret "-" et 4 chiffres
* Voir http://www.usps.gov/ncsc/lookups/addressing_definitions.htm#ZIPCode
*
* Nota: attention au "-" en envoyant le paramètre ($code). Contrôler avant envoi
* qu'il s'agit bien d'une string, sinon PHP prend le "-" pour un
* simple opérateur entre deux entiers.
*/
function kzo_is_zipcode ($code="") {
return (ereg ("^[[:digit:]]{5,5}(-[[:digit:]]{4,4})?$", $code));
}
//$test="12345-7890"; echo ($test.": ".(kzo_is_zipcode($test)?"TRUE":"FALSE"));
?>
Version originale trouvée en forum php.net, adaptée pour retourner un booléen.
<?php
function is_utf8($string) {
// Returns true if $string is valid UTF-8, false otherwise.
return
(preg_match('%^(?:
// From http://www.w3.org/International/questions/qa-forms-utf-8.en.php
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)*$%xs', $string)
? TRUE : FALSE);
}
?>
Les forums sont fermés.