# Posted By

serialk89 on 06/17/11

# Statistics

Viewed 195 times
Favorited by 0 user(s)

# VALIDAR EMAIL Y STRING CON PHP

/ Published in: PHP
`function comprobar_string(\$string){        //compruebo que los caracteres sean los permitidos        \$permitidos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_Ã¡Ã©Ã­Ã³ÃºÃÃ‰ÃÃ“Ãš";        for (\$i=0; \$i<strlen(\$string); \$i++){            if (strpos(\$permitidos, substr(\$string,\$i,1))===false){                \$error = "novalido".\$string;                return \$error;            }        }        return \$string;    }      function comprobar_email(\$email){        \$mail_correcto = 0;        //compruebo unas cosas primeras        if ((strlen(\$email) >= 6) && (substr_count(\$email,"@") == 1) && (substr(\$email,0,1) != "@") && (substr(\$email,strlen(\$email)-1,1) != "@")){            if ((!strstr(\$email,"'")) && (!strstr(\$email,"\"")) && (!strstr(\$email,"\\")) && (!strstr(\$email,"\\$")) && (!strstr(\$email," "))) {                //miro si tiene caracter .                if (substr_count(\$email,".")>= 1){                    //obtengo la terminacion del dominio                    \$term_dom = substr(strrchr (\$email, '.'),1);                    //compruebo que la terminaciÃ³n del dominio sea correcta                    if (strlen(\$term_dom)>1 && strlen(\$term_dom)<5 && (!strstr(\$term_dom,"@")) ){                        //compruebo que lo de antes del dominio sea correcto                        \$antes_dom = substr(\$email,0,strlen(\$email) - strlen(\$term_dom) - 1);                        \$caracter_ult = substr(\$antes_dom,strlen(\$antes_dom)-1,1);                        if (\$caracter_ult != "@" && \$caracter_ult != "."){                            \$mail_correcto = 1;                        }                    }                }            }        }        if (\$mail_correcto)            return \$email;        else            return "novalido".\$email;    }`