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 (:
 

 

Link to comment
Share on other sites

I guess this does what you want?

 

// match <td>some@email.com</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 Edited by ignace
Link to comment
Share on other sites

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>";

?>


 





 

Edited by phpuser2013
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.