user1414880
user1414880

Reputation:

grabbing website from email address in php

How to pull website name from email address in php/mysql. Exploding is too expensive here ,as i have to do it for several thousands data inside db

original data
[email protected]
[email protected]

needed data
example.com
example1.com

Upvotes: 1

Views: 94

Answers (6)

PenguinCoder
PenguinCoder

Reputation: 4357

Just for something a bit different: preg_match domain name capture:

<?php
  $address = array("[email protected]","[email protected]","[email protected]");
  foreach ($address as $email) {
   preg_match(";\b.+@([a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,}))\b;", $email,$matches);
   echo $matches[1];
   echo "\n";
  }
?>

Output:

example.com
example.co.uk
ez.example.com

Upvotes: 0

FThompson
FThompson

Reputation: 28687

Find the index of the @ by using strrpos and then substring the remaining part using substr.

$atpos = strrpos($eaddress, '@');
$domain = substr($eaddress, $atpos + 1);

Upvotes: 1

Will
Will

Reputation: 20235

function getDomain( $email ) {
    return substr( $email, strpos( $email, '@' ) + 1 );
}

$website = getDomain( '[email protected]' );

Upvotes: 3

Imran
Imran

Reputation: 3024

If you have long list of email in Database then its better to trim website from database rather than on web server Query should be

SELECT SUBSTRING_INDEX(email_field, '@', -1) FROM email_table;

because of -1 right side string would be return after first occurrence of @

Hopes that Helps

Upvotes: 1

Rawkode
Rawkode

Reputation: 22592

foreach(... as $email) {
  $domains[] = substr($email, strpos('@', $email) + 1);
}

This should work nicely - note that there is no validation for strpos returning false, indicating that the line does not contain the @ symbol.

Upvotes: 1

woz
woz

Reputation: 10994

Use strpos to find the @ and substr to get the part after it.

Upvotes: 1

Related Questions