{"id":41,"date":"2015-08-27T06:21:11","date_gmt":"2015-08-27T04:21:11","guid":{"rendered":"http:\/\/lab.fawno.com\/?p=41"},"modified":"2022-03-07T00:56:00","modified_gmt":"2022-03-06T23:56:00","slug":"","status":"publish","type":"post","link":"https:\/\/lab.fawno.com\/en\/2015\/08\/27\/validacion-de-cif-nif\/","title":{"rendered":"","raw":""},"content":{"rendered":"","protected":false,"raw":""},"excerpt":{"rendered":"","protected":false,"raw":""},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_editorskit_title_hidden":false,"_editorskit_reading_time":0,"_editorskit_typography_data":[],"_editorskit_blocks_typography":"","_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","_es_post_content":"\n
Cuando se necesita validar un CIF-NIF lo primero que te viene a la cabeza es que hay varios tipos, no me voy a explayar, esta todo muy bien explicado en la Wikipedia: <\/p>\n\n\n\n
El caso es que es la t\u00edpica historia que empaquetas en una funci\u00f3n y te olvidas: <\/p>\n\n\n\n
function cif_validation ($cif) {\n $cif = strtoupper($cif);\n if (preg_match('~(^[XYZ\\d]\\d{7})([TRWAGMYFPDXBNJZSQVHLCKE]$)~', $cif, $parts)) {\n $control = 'TRWAGMYFPDXBNJZSQVHLCKE';\n $nie = array('X', 'Y', 'Z');\n $parts[1] = str_replace(array_values($nie), array_keys($nie), $parts[1]);\n $cheksum = substr($control, $parts[1] % 23, 1);\n return ($parts[2] == $cheksum);\n } elseif (preg_match('~(^[ABCDEFGHIJKLMUV])(\\d{7})(\\d$)~', $cif, $parts)) {\n $checksum = 0;\n foreach (str_split($parts[2]) as $pos => $val) {\n $checksum += array_sum(str_split($val * (2 - ($pos % 2))));\n }\n $checksum = ((10 - ($checksum % 10)) % 10);\n return ($parts[3] == $checksum);\n } elseif (preg_match('~(^[KLMNPQRSW])(\\d{7})([JABCDEFGHI]$)~', $cif, $parts)) {\n $control = 'JABCDEFGHI';\n $checksum = 0;\n foreach (str_split($parts[2]) as $pos => $val) {\n $checksum += array_sum(str_split($val * (2 - ($pos % 2))));\n }\n $checksum = substr($control, ((10 - ($checksum % 10)) % 10), 1);\n return ($parts[3] == $checksum);\n }\n return false;\n}<\/pre>\n\n\n\nEl funcionamiento es tan sencillo como llamar a la funci\u00f3n con un CIF-NIF y te devuelve true o false seg\u00fan sea v\u00e1lido o no.<\/p>\n\n\n\n
Saludos<\/p>\n","_es_post_name":"validacion-de-cif-nif","_es_post_excerpt":"","_es_post_title":"Validaci\u00f3n de CIF-NIF","_en_post_content":"","_en_post_name":"","_en_post_excerpt":"","_en_post_title":"","edit_language":"en","footnotes":""},"categories":[5,3],"tags":[8,9,6,7],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-php","category-tips","tag-cif","tag-nif","tag-php","tag-validacion"],"ninja_gutenberg_blocks_featured_media_urls":{"thumbnail":"","ninja_gutenberg_blocks_landscape_large":"","ninja_gutenberg_blocks_portrait_large":"","ninja_gutenberg_blocks_square_large":"","ninja_gutenberg_blocks_landscape":"","ninja_gutenberg_blocks_portrait":"","ninja_gutenberg_blocks_square":"","full":""},"_links":{"self":[{"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":19,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":1846,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/posts\/41\/revisions\/1846"}],"wp:attachment":[{"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}