《PHP教程:php+xml實(shí)現(xiàn)在線英文詞典查詢的方法》要點(diǎn):
本文介紹了PHP教程:php+xml實(shí)現(xiàn)在線英文詞典查詢的方法,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
PHP學(xué)習(xí)本文實(shí)例講述了php+xml實(shí)現(xiàn)在線英文詞典查詢的辦法.分享給大家供大家參考.具體如下:
PHP學(xué)習(xí)這里的xml相當(dāng)于一個(gè)數(shù)據(jù)庫(kù).實(shí)現(xiàn):查詢某個(gè)英文單詞,輸出它的中文意思.
PHP學(xué)習(xí)xml文件(數(shù)據(jù)庫(kù)):words.xml如下:
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<words>
<word>
?<en>boy</en>
?<ch>男孩</ch>
</word>
<word>
?<en>girl</en>
?<ch>女孩</ch>
</word>
<word>
?<en>teacher</en>
?<ch>老師</ch>
</word>
<word>
?<en>beauty</en>
?<ch>美女</ch>
</word>
</words>
PHP學(xué)習(xí)查詢文件:word.php
代碼如下:
<h2>在線英漢詞典</h2>
<form action="xmlprocess.php" method="post">
請(qǐng)輸入英文單詞:<input type="text" name="enword" />
<input type="submit" value="查詢" name="sub">
</form>
PHP學(xué)習(xí)處理文件:xmlprocess.php
代碼如下:
<?php
//創(chuàng)建xml對(duì)象
$xmldoc = new DOMDocument();
$xmldoc->load("words.xml");
//查詢
if(!empty($_POST['sub'])){
?$en_word = $_POST['enword'];
?$word = $xmldoc->getElementsByTagName("en");
?for($i=0;$i<$word->length;$i++){
? if($en_word==$word->item($i)->nodeValue){
?? $cn_word = $xmldoc->getElementsByTagName("ch")->item($i)->nodeValue;
?? break;
? }else{
?? $cn_word = "找不到你所輸入的單詞";
? }
?}
}
echo $cn_word;
?>
PHP學(xué)習(xí)希望本文所述對(duì)大家的php操作XML程序設(shè)計(jì)有所贊助.
歡迎參與《PHP教程:php+xml實(shí)現(xiàn)在線英文詞典查詢的方法》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/12552.html