adapt function filter_var () to php5.1.6 with preg_match_all ()

0

I realized that the filter_var() function does not email with my php 5.1.6

which gives me the following error: Call to undefined function filter_var()

my code is:

 while ( $row = mysql_fetch_assoc( $result ) ) {
                    if( filter_var($row['EMAIL1_CLI'], FILTER_VALIDATE_EMAIL) ){
                        //$return .= '<label>Correo enviado a ' . $row['EMAIL1_CLI'] . '</label></br>'; 
                        $html .= enviar_mail( $row, $row['EMAIL1_CLI'] );   

                    }elseif( filter_var($row['EMAIL2_CLI'], FILTER_VALIDATE_EMAIL)  ){
                        //$return .= '<label>Correo enviado a ' . $row['EMAIL2_CLI'] . '</label></br>'; 
                        $html .= enviar_mail( $row, $row['EMAIL2_CLI'] );   

                    }else{
                        $html .= '<h4 style="text-align:center; color: red;">Ninguno de los correos es valido</h4>';
                    }

the code needs to validate the (valid) emails that are in the field EMAIL1_CLI and EMAIL2_CLI of the BD, and then send them through the function.

someone can help me adapt it so that it runs in php 5.1.6

try with preg_match_all :

 $regexp = '/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';

 while ( $row = mysqli_fetch_array( $result ) ) {
        $mail1  = $row['EMAIL1_CLI'];
        $mail2  = $row['EMAIL2_CLI'];
        $datos  = array();
        $datos['EMAIL1_CLI'] = $mail1;
        $datos['EMAIL2_CLI'] = $mail2;

        preg_match_all($regexp, $mail1, $matches);
        preg_match_all($regexp, $mail2, $matches2);


        if( filter_var($row['EMAIL1_CLI'], FILTER_VALIDATE_EMAIL) ){
            //$return .= '<label>Correo enviado a ' . $row['EMAIL1_CLI'] . '</label></br>'; // mostrar correos enviados
            $html .= enviar_mail( $datos, trim( $matches[0][0]) );  

        }elseif( filter_var($row['EMAIL2_CLI'], FILTER_VALIDATE_EMAIL)  ){
            //$return .= '<label>Correo enviado a ' . $row['EMAIL2_CLI'] . '</label></br>'; // mostrar correos enviados
            $html .= enviar_mail( $datos, trim( $matches2[0][0]) ); 

        }else{
            $html .= '<h4 style="text-align:center; color: red;">Ninguno de los correos es valido</h4>';
        }
        //$return .= '<hr>'; //espacios entre codigos enviados a mostrar


    }
    
asked by Francisco Acevedo 30.10.2018 в 15:54
source

1 answer

0

With regular expressions it would be something like this:

<?php

$regexp = '/[a-z\d._%+-]+@[a-z\d.-]+\.[a-z]{2,4}\b/i';

$emails = array(array('EMAIL1_CLI'=>'test#gmail.com', 'EMAIL2_CLI'=>'[email protected]'));
$html = "";
foreach($emails as $row) {
    $mail1  = $row['EMAIL1_CLI'];
    $mail2  = $row['EMAIL2_CLI'];
    $matches  = array();
    $matches2 = array();
    preg_match_all($regexp, $mail1, $matches);
    preg_match_all($regexp, $mail2, $matches2);

    if( !empty($matches[0]) ){
        $html .= '<label>Correo enviado a ' . $row['EMAIL1_CLI'] . '</label></br>'; 

    }elseif( !empty($matches2[0])  ){
        $html .= '<label>Correo enviado a ' . $row['EMAIL2_CLI'] . '</label></br>'; 

    }else{
        $html .= '<h4 style="text-align:center; color: red;">Ninguno de los correos es valido</h4>';
    }

}
echo $html;
?>
    
answered by 30.10.2018 в 16:34