[英]How can i fetch required data from a string using php
otpauth:// totp / Dropbox:myemail@gmail.com?secret = MYSECRETCODE&issuer = Dropbox
otpauth://totp/myemail@google.com?secret = MYSECRETCODE
otpauth://totp/github.com/MyUsername?issuer = GitHub&secret = MySecret
我需要變量中的密碼,電子郵件,發行人,標簽。 我已經使用explode進行了編碼,但是它不能是第三個。 有沒有使用regex獲取信息的簡單方法?
$data = $request['code'];
$remove_rules = substr($data, 15);
$que_exp = explode('?',$remove_rules);
if(array_key_exists(1,$que_exp))
{
if (filter_var($que_exp[0], FILTER_VALIDATE_EMAIL)) {
$email = $que_exp[0];
}
}
$secret_exp = explode('&',$que_exp[1]);
$secret = substr($secret_exp[0], strpos($secret_exp[0], "=") + 1);
if(array_key_exists(1,$secret_exp))
{
$issuer = substr($secret_exp[1], strpos($secret_exp[1], "=") + 1);
}
$label_email = explode(':',$que_exp[0]);
if (!filter_var($label_email[0], FILTER_VALIDATE_EMAIL)) {
if (!filter_var($label_email[1], FILTER_VALIDATE_EMAIL)) {
return 'data error';
}
$email = $label_email[1];
$label = $label_email[0];
}
else
$email = $label_email[0];
if(empty($issuer)) {
if(empty($label))
$issuer = "Account";
else
$issuer = $label;
}
您可以使用http://php.net/manual/en/function.parse-url.php解析URI,然后分解查詢鍵值對。 這比使用正則表達式更正確。
嘗試這個:
print_r(parse_url('otpauth://totp/github.com/MyUsername?issuer=GitHub&secret=MySecret'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.