Questo è molto più facile (dopo aver verificato che l'e-mail è valido):
$email = '[email protected]';
$split = explode('@',$email);
$name = $split[0];
echo "$name"; // would echo "my.name"
Per verificare la validità, si potrebbe fare questo:
function isEmail($email) {
return (preg_match('/[\w\.\-][email protected][\w\.\-]+\.\[w\.]/', $email));
}
if (isEmail($email)) { ... }
Come per estrarre l'e-mail di First Last <[email protected]>
,
function returnEmail($contact) {
preg_match('\b[\w\.\-][email protected][\w\.\-]+\.\[w\.]\b', $contact, $matches);
return $matches[0];
}
Osservando le risposte, è possibile notare che '" e \ @w "@ example.com' è, tecnicamente, un indirizzo email valido. – Erik
Appena realizzato c'era molto di più nella stringa oltre al semplice indirizzo email. –
È possibile trovare ['s ($ str) -> beforeLast ('@')'] (https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str.php#L399) utile, come trovato in [questa libreria standalone] (https://github.com/delight-im/PHP-Str). – caw