jproux
jproux

Reputation: 265

Remove Portion of Email and Remove Duplicates

I have a text file with various names, tags and emails in it. I have managed to extract the data so that only the emails show in a list. Now I need to remove the string data up to the @ symbol. In other words only the domain part (like domain.com) should be left.

Then after that I need to remove the duplicate domains. How would I do this last bit of the exercise? This is the php code I have so far:

<?php

$string = file_get_contents('text.txt');
$pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i';
$result = preg_match_all($pattern, $string, $matches);
if($result) {
    foreach(array_unique($matches[0]) as $email) {
        echo $email . "<br />";
    } 
}

Edit: Ok, so I added the following code to my if statement:

if($result) {
    foreach(array_unique($matches[0]) as $email) {
        $domain = strstr($email, '@', false);
        $domain1 = str_replace('@', '', $domain);
        echo $domain1 . '<br />';
    } 
}

This gives me the domain part of the email. Now I need to 1. remove all duplicates and 2. sort alphabetically by domain name

It does not seem as if I can use the array_unique() function as this is not an array. Any ideas?

Upvotes: 0

Views: 191

Answers (2)

jproux
jproux

Reputation: 265

At the end of the day, this was the code that did the trick

<?php

$string = file_get_contents('text.txt');
$pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i';
$result = preg_match_all($pattern, $string, $matches);
$domains = [];
if($result) {
    foreach(array_unique($matches[0]) as $email) {
    $domain = strstr($email, '@', false);
    $domain1 = str_replace('@', '', $domain);
    $domains[$domain1] = $domain1;
} 
asort($domains);
foreach($domains as $domain) {
    echo $domain . '<br />';
}
}

Upvotes: 0

Shahnawaz Kadari
Shahnawaz Kadari

Reputation: 1571

You can do it three ways....

Regular Expression

$email  = '[email protected]';
preg_match("/\@(.*)/", $email, $domain);
echo $domain[1];

Using explode

$email  = '[email protected]';
$domain = explode ("@", $email);
echo $domain[1];

Using substr

$email  = '[email protected]';
$domain = substr($email, strpos($email, "@") + 1);
echo $domain;

All of above gives same output.

domain.com

Upvotes: 1

Related Questions