Reputation:
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
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
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
Reputation: 20235
function getDomain( $email ) {
return substr( $email, strpos( $email, '@' ) + 1 );
}
$website = getDomain( '[email protected]' );
Upvotes: 3
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
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