在我的注册表单中,我要求用户输入一个与他们在上面的网址字段中输入的域名相同的电子邮件。
现在,我以这种方式收集数据:
网址:http://www.
domain.com domain.com部分是用户输入的内容。 http://www
是硬编码的。
电子邮件:信息 @ domain.com 用户输入粗体部分。 @ 是硬编码的。
电子邮件中url和domain.com部分中的domain.com部分应该匹配。现在,我可以匹配这两个字段,因为它们是分开的。
但我想放弃上述方法并让用户输入整个域名和电子邮件。在这种情况下,检查用户是否输入了他在上面的网址字段中输入的相同域名的电子邮件的好方法。
我正在使用php完成所有这些。
答案 0 :(得分:1)
您可以explode()
supp url = bla@gmail.com
$pieces = explode("@", $url);
$new = $pieces[1]; //which will be gmail.com
现在再次爆炸
$newpc= explode(".", $new );
$new1 = $newpc[0]; //which will be gmail
答案 1 :(得分:1)
<?php
//extract domain from email
$email_domain_temp = explode("@", $_POST['email']);
$email_domain = $email_domain_temp[1];
//extract domain from url
$url_domain_temp = parse_url($_POST['url']);
$url_domain = strip_out_subdomain($url_domain_temp['host']);
//compare
if ($email_domain == $url_domain){
//match
}
function strip_out_subdomain($domain){
//do nothing if only 1 dot in $domain
if (substr_count($domain, ".") == 1){
return $domain;
}
$only_my_domain = preg_replace("/^(.*?)\.(.*)$/","$2",$domain);
return $only_my_domain;
}
所以它的作用是:
首先,将电子邮件字符串拆分为数组中的两部分。第二部分是域名。
其次,使用php内置函数解析url,然后提取“host”,同时删除(optionnal)子域。
然后比较。
答案 2 :(得分:1)
这是我的版本(已测试,有效):
<?php
$domain = 'www2.example.com'; // Set domain here
$email = 'info@example.com'; // Set email here
if(!preg_match('~^https?://.*$~i', $domain)) { // Does the URL start with http?
$domain = "http://$domain"; // No, prepend it with http://
}
if(filter_var($domain, FILTER_VALIDATE_URL)) { // Validate URL
$host = parse_url($domain, PHP_URL_HOST); // Parse the host, if it is an URL
if(substr_count($host, '.') > 1) { // Is there a subdomain?
$host = substr($host, -strrpos(strrev($host), '.')); // Get the host
}
if(strpos(strrev($email), strrev($host)) === 0) { // Does it match the end of the email?
echo 'Valid!'; // Valid
} else {
echo 'Does not match.'; // Invalid
}
} else {
echo 'Invalid domain!'; // Domain is invalid
}
?>
答案 3 :(得分:0)
$parsedUrl = parse_url($yourEnteredUrl);
$domainHost = str_replace("www.", "", $parsedUrl["host"]);
$emailDomain = array_pop(explode('@', $yourEnteredEmail));
if( $emailDomain == $domainHost ) {
//valid data
}
答案 4 :(得分:0)
使用具有正向lookbehinds的正则表达式(即,如果前面有某个模式,则仅返回我想匹配的表达式,但在匹配中不包括 lookbehind本身),像这样:
<?php
$url = preg_match("/(?<=http:\/\/www\.).*/",$_POST['url'],$url_match);
$email = preg_match("/(?<=@).*/",$_POST['email'],$email_match);
if ($url_match[0]==$email_match[0]) {
// Success Code
}
else {
// Failure Code
}
?>
当然这有点过于简单,因为您还需要考虑https或www2等等,但这些仅需要对RegExp进行微小更改,使用问号作为“可选”运算符
答案 5 :(得分:0)
$email = 'myemail@example.com';
$site = 'http://example.com';
$emailDomain = ltrim( strstr($email, '@'), '@' );
// or automate it using array_map(). Syntax is correct only for >= PHP5.4
$cases = ['http://'.$emailDomain, 'https://'.$emailDomain, 'http://www.'.$emailDomain, 'https://www.'.$emailDomain];
$bSameDomain = in_array($site, $cases);
var_dump($bSameDomain);