Jump to content

Make of 2 regular expressions 1 regular expression


Recommended Posts

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

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 */
                                    $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
	"UK"=>"^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$",
	"DK"=>"^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$",


for($i=0,$max = count($result); $i < $max; $i++) {
                                                         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                                                                                                                             
                                                                                                                                                                                                   //trying to read fields
                                                        } else {
                                                            //Validation not available

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

    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.

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.