Jump to content

Make of 2 regular expressions 1 regular expression


phpuser2013

Recommended Posts

Hi,
I would like to have one expression:

//1. expression
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';   //verify email

//2. expression
preg_match_all('~<(td)>(?<content>\w+)</\1>~Uis', $gesamteDatei, $result, PREG_SET_ORDER); //get values of the data set, which are
// the 2. expression doesnt match any email adress

I tried it, but I failed and I dont know why... may be you can help me ?


preg_match_all('~<(td)>(?<content>/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/)</\1>~Uis', $gesamteDatei, $result, PREG_SET_ORDER);

Thanks for your help (:
 

 

I guess this does what you want?

 

// match <td>[email protected]</td>
if (preg_match_all('~<(td)>(?<content>[_a-z0-9-]+(?:\.[_a-z0-9-]+)*@[a-z0-9-]+(?:\.[a-z0-9-]+)*(?:\.[a-z]{2,3}))</\1>~Uis', $gesamteDatei, $matches)) {
  foreach ($matches['content'] as $email) {
    //
  }
}
If you want to test your regular expressions, you can do so here: http://www.solmetra.com/scripts/regex/index.php

Thanks for your help so far,

I got a file where i want

...<tr><td>value 1</td><td>value 1</td><td>value 2</td><td>value 3</td><td>value 4</td><td>value 5</td><td>value 6</td><td>email</td><td>value 8</td></tr> ... //1 dataset

The problem is that the value is mostly at a different position:

like ->

...<tr><td>value 1</td><td>value 1</td><td>value 2</td><td>value 3</td><td>value 4</td><td>email</td><td>value 6</td><td>value 7</td><td>value 8</td></tr> ... //1 dataset


or like ->


...<tr><td>value 1</td><td>value 1</td><td>value 2</td><td>value 3</td><td>value 4</td><td>value 5</td><td>value 6</td><td>value 7</td><td>email</td></tr> ... //1 dataset

and I need some of the value left or right of that field,

I tried to solve it like this:

 preg_match_all('/<td>(.*?)<\/td>/',$gesamteDatei,$InhaltTdElemente);  //find values

$gesamteDatei= whole stream(1 table and many  datasets)
$InhaltTdElemente= all values between <td> </td>


I need to search for something"(ZiP, Country....)

preg_match_all('~<(td)>(?<content>\w+)</\1>~Uis', $gesamteDatei, $result, PREG_SET_ORDER);

// that doesnt give me the email address
 

<?php
print "<html>";
print "<head>";
print "</head>";
print "<body>";
print "<nobr>"; 

$datei='new_folders42000.html';  // stream
$pfad='/xampp/htdocs/xampp/new/'; //path to stream
 if($gesamteDatei=file_get_contents($datei)){ //checking access
    printf(" <div style='background:lime'>Datei kann ausgelesen werden !</div><br>\n"); //reading ok
 
preg_match_all('/<td>(.*?)<\/td>/',$gesamteDatei,$InhaltTdElemente);  //whole content
preg_match_all('~<(td)>(?<content>\w+)</\1>~Uis', $gesamteDatei, $result, PREG_SET_ORDER); //
 
 /*checking email */
$lange=count($InhaltTdElemente[1]);
for($i=0;$i<$lange;$i++){
                                    $email = $InhaltTdElemente[1][$i]; // alle Felder
                                     $regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'; 
                                     
                                                            if (preg_match($regex, $email)) {
                                                                 echo $email.'<br/>';
                                                                                                                      
                                                            } else { 
                                                                 //echo $email ." is an invalid email. Please try again.";
                                                            } 
}

echo '<br/>';
echo '<br/>';
  


$country_code="DE"; //regular expression germany

//Checking zip_clode
$ZIPREG=array(
	"US"=>"^\d{5}([\-]?\d{4})?$",
	"UK"=>"^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$",
	"DE"=>"\b(?!01000)(?!99999)(0[1-9]\d{3}|[1-9]\d{4})\b",
	"CA"=>"^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$",
	"FR"=>"^(F-)?((2[A|B])|[0-9]{2})[0-9]{3}$",
	"IT"=>"^(V-|I-)?[0-9]{5}$",
	"AU"=>"^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$",
	"NL"=>"^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$",
	"ES"=>"^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$",
	"DK"=>"^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$",
	"SE"=>"^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$",
	"BE"=>"^[1-9]{1}[0-9]{3}$"
);
 

 
 

  
for($i=0,$max = count($result); $i < $max; $i++) {
 $zip_postal=$result[$i]['content'];
 
                                                         if ($ZIPREG[$country_code]) { //at the moment germany
                                                         
                                                                                                                                        if (!preg_match("/".$ZIPREG[$country_code]."/i",$zip_postal)){  //check german zip
                                                                                                                                            //Validation failed, provided zip/postal code is not valid.
                                                                                                                                        } else {
                                                                                                                                            //Validation passed, provided zip/postal code is valid.
                                                                                                                                          if($result[$i]['content'] >=50000 && $result[$i]['content'] <= 60000) {       //range of zip                                                                                                                             
                                                                                                                                                                                                        //$datei=fopen($pfad.'56000.html','w+');
                                                                                                                                                                                                   //trying to read fields
                                                                                                                                                                                                        printf($result[$i]['content'].'<br/>');
                                                                                                                                                                                                        printf($result[$i-1]['content'].'<br/>');
                                                                                                                                                                                                        printf($result[$i-2]['content'].'<br/>');
                                                                                                                                                                                                        printf($result[$i-3]['content'].'<br/>');
                                                                                                                                                                                                        printf($result[$i-4]['content'].'<br/>');
                                                                                                                                                                                                        printf($result[$i-5]['content'].'<br/>');
                                                                                                                                                                                                        printf($result[$i-6]['content'].'<br/>');
                                                                                                                                                                                                        printf($result[$i-7]['content'].'<br/>');
                                                                                                                                                                                                        printf($result[$i-8]['content'].'<br/>');
                                                                                                                                                                                                        printf($result[$i-9]['content'].'<br/>');
                                                                                                                                                                                                        printf($result[$i+1]['content'].'<br/>');
                                                                                                                                                                                                        printf($result[$i+2]['content'].'<br/>');
                                                                                                                                                                                                        printf($result[$i+3]['content'].'<br/>');
                                                                                                                                                                                                        printf($result[$i+4]['content'].'<br/>');
                                                                                                                                                                                                        printf('<br>');
                                                                                                                                                                                                        //fclose($datei);
                                                                                                                                                                                            } 
                                                                                                                                                                                                            
                                                                                                                                     }
                                                                                                                                     
                                                        } else {
                                                         
                                                            //Validation not available
                                                         
                                                        }

}
 
// if ( preg_match('/^\d{5}$/', $input) && (int) $input > 1000 && (int) $input < 99999 ) {}
//fclose($datei);
}

}
}else{
    printf(" <div style='background:red'>Datei kann nicht ausgelesen werden !</div><br>\n");
}


print "</nobr>";
print "</body>";
print "</html>";

?>


 





 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.