Javascript 如何读取通过表单输入的电子邮件地址的域名
我有一个登录表单,我希望能够提取电子邮件地址输入的域名,然后用一个新的变量覆盖预定义的变量 这是我的代码:Javascript 如何读取通过表单输入的电子邮件地址的域名,javascript,php,html,Javascript,Php,Html,我有一个登录表单,我希望能够提取电子邮件地址输入的域名,然后用一个新的变量覆盖预定义的变量 这是我的代码: <form action="example.php" method="POST"> <input type="text" name="input_value"> //assume I input gooogle@gmail.com <input type="hidden" name="isp" id="isp" value="unknown" /> &l
<form action="example.php" method="POST">
<input type="text" name="input_value"> //assume I input gooogle@gmail.com
<input type="hidden" name="isp" id="isp" value="unknown" />
<input type="submit" name="submit">
//假设我输入gooogle@gmail.com
example.php:
<?php
$isp = $_POST["isp"];
//rest of code
?>
我希望能够读取域并将变量$isp转换为我设置的预定义值,例如'gmail.com'='gmail',因此
$isp='gmail'
您确实应该依赖使用c-client
API的php imap模块中的imap\u rfc822\u parse\u adrlist
。它处理一个逗号分隔的列表,返回一个对象数组,并允许格式用户可能重复的“如何仅从电子邮件地址获取“用户”?有没有简单的方法在@之前获取值?”我不认为这是重复的,这篇文章只是一个标题不好的问题,虽然他可以在那里得到他的答案,但这还不到复制的50%,你有没有读过我的帖子并将其与另一篇帖子进行比较?有很多不同,伙计。伙计,结果和你要找的一样。把结果颠倒过来…但是好的,也许很难…你说的对。可能是酷酷答案的复制品它肯定在做我需要它做的事情,但我希望能够做到这一点,而不必安装任何软件dependencies@WilliamsHarold再次更新,以鼓励您尽可能依赖imap\u rfc822\u parse\u adrlist
。它确实有用,但我正在制作模板对于多个用户,无需开始解释如何以及为什么他们必须安装imap\u rfc822\u parse\u adrlist
@WilliamSharld这就是为什么有行定义('ALLOW\u FALLBACK\u EMAIL\u CHECK',true)代码>允许在未启用IMAP的情况下使用回退版本。您可以取消注释ist,但是,默认情况下应注释掉将责任转移给最终用户。安装可以根据用户选择自动进行此类设置。在PHP中没有真正实现良好的替代方案。
<?php declare (strict_types=1);
// This should NOT be enabled. If possible, ensure to have installed the php-imap module.
//
// define('ALLOW_FALLBACK_EMAIL_CHECK', true);
if(function_exists('imap_rfc822_parse_adrlist'))
{
/**
* Get the host part (domain) of an email address.
*
* @param string|null $email email address, can be null
* @return string|null host part of the email address if the address is valid, otherwise NULL
*/
function get_host_from_email_addr(?string $email) : ?string
{
if(null === $email)
return null;
@[$obj, $obj2] = imap_rfc822_parse_adrlist($email, '');
imap_errors(); // flush errors, otherwise unsuppressable notifications on wrong format are shown due to external calls
// we want to allow 'simple email addresses' only and therefore exact the 2 properties:
// ->mailbox and ->host
return (isset($obj->mailbox, $obj->host) && !isset($obj2) && 2 === count(get_object_vars($obj)))
? $obj->host ?: null
: null
;
}
}
else if(defined('ALLOW_FALLBACK_EMAIL_CHECK') && ALLOW_FALLBACK_EMAIL_CHECK)
{
function get_host_from_email_addr(?string $email) : ?string
{
// This probably ensures getting a valid host name from email address.
// However, filter_var works too restrictive generating false negatives.
return filter_var($email, FILTER_VALIDATE_EMAIL, FILTER_FLAG_EMAIL_UNICODE) ? array_slice(explode('@', $email), -1)[0] : null ;
}
}
else
throw new Error('Unresolved dependency: Please install php-imap module or set constant ALLOW_FALLBACK_EMAIL_CHECK to TRUE.');
$isp_names =
[
'gmail.com' => 'Gmail',
'outlook.com' => 'Outlook',
];
$isp = @$_GET['isp'] ?: $isp_names[get_host_from_email_addr(@$_GET['input_value'])] ?? 'unknown';