I am trying to obtain the local part of an email ID using regex. The challenge here is that the local part comes in two different formats and I need to figure out which format I'm reading and prepare the alternate form of that email ID. As always the snippet of my code that does this is pasted below.
$testarray = array("[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]");
foreach($testarray as $emailID) {
preg_match("/([\w\d]*)\.([\w\d]*)@gmail.com/", $emailID, $match);
if ($match[2] == "tp") {
$altform = $match[1] . "@gmail.com";
} else {
$altform = $match[1] . "[email protected]";
}
error_log("ALTERNATE FORM OF $emailID IS $altform");
}
The problem I'm facing here is I'm not getting the desired result as neither $match[1] and $match[2] match anything for "[email protected]".
\.character, you should test ifpreg_match()actually worked or not before trying to consume$match