dsb53973 2014-10-11 08:12
浏览 35
已采纳

如何检查xml文件中是否存在电子邮件[重复]

This question already has an answer here:

I want the system to check whether the email exists in the xml file when a new registration occurs, how can I do that? I got the basic idea of comparing all the email nodes but still cant get it to work after several hours. Thanks for the help

register.html

<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
</head>
<body>
<h1>ShipOnline System Registration Page</h1>
<form id="regform" method="post" action="register.php">
<fieldset id="person"> 
<legend>Your details:</legend>
<p><label for="fname">First Name:</label><input type="text" name="fname"/></p>
<p><label for="lname">Last Name:</label><input type="text" name="lname"/></p>
<p><label for="password">Password:</label><input type="password" name="password"/></p>
<p><label for="cpassword">Confirm Password:</label><input type="password" name="cpassword"/></p>            
<p><label for="email">Email:</label><input type="email" id="email" name="email"/></p>
<p><label for="phone">Phone:</label><input type="text" name="phone"/></p>
<input type="submit" value="Register"/>
</fieldset>
</form>
</body>
</html>

relevant parts of register.php

<?php
$email = @trim($_POST["email"]);
if ($password != $cpassword)
{
    echo 'Password does not match';
}
@override
else if ($email /*exists*/)
{
    echo 'Email exists';
}
?>

customer.xml

<?xml version="1.0"?>
<customer>
<user>
    <id>0</id>
    <fname>John</fname>
    <lname>Smith</lname>
    <email>jsmith@gmail.com</email>
    <password>jsmith</password>
    <phone>0412345677</phone>
</user>
<user>
    <id>1</id>
    <fname>Arthur</fname>
    <lname>Luo</lname>
    <email>aluo@gmail.com</email>
    <password>aluo</password>
    <phone>0412345678</phone>
</user>

</div>
  • 写回答

1条回答 默认 最新

  • doucuan5365 2014-10-11 08:21
    关注

    The easiest way is to do a simple search of the contents of your XML file. This is fine if you are confident that there are not other <email> tags containing the email address in some other context. For example:

    # slurp the XML file into a variable
    $xml = file_get_contents('customer.xml');
    # search for the email address within the tags
    if (strpos($xml, "<email>$email</email>") !== false)) {
        # you've found it!
    }
    

    You can also use DOMDocument and DOMXPath to parse the XML and search for the email address. To be honest, this seems like overkill given the simplicity of the XML file you've posted.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式