Basit Regex Yardım

3 Cevap php

Nasıl Regex kullanarak bir dize içinde e-posta kapmak mı?

Aşağıdaki gibi benim dize

"First Last <first.last@email.com>"

Ben "first.last @ gmail.com" yakala ve bir yere saklamak istiyoruz.

Şimdiden teşekkürler!

3 Cevap

Regex olmadan (ve muhtemelen çok daha hızlı):

$string = "First Last <first.last@email.com>";
echo substr($string, strpos($string, '<') +1, -1);

veya

echo trim(strstr("First Last <first.last@email.com>", '<'), '<>');

hem de verecektir

first.last@email.com

Eğer nihai sonucu doğrulamak gerekiyorsa kullanın

filter_var($eMailString, FILTER_VALIDATE_EMAIL);

Sizin örnekte, ben böyle bir şey yapacağım:

preg_match('/<([^>]+)>/', "First Last <first.last@email.com>", $matches);
$email = $matches[1];

Çıkış the official PHP documentation on preg_match.