PHP:基於XML的線上小詞典案例

來源:互聯網
上載者:User
html><html lang="en"><head> <meta charset="gb2312"> <title>dictionarytitle>head><img src="view.jpg" width="400px" /><h1>查詢單詞h1><form action="wordProcess.php" method="post" > 請輸入單詞:<input type="text" name="enword" /> <input type="hidden" name="type" value="query"> <input type="submit" value="查詢">form><h1>添加單詞h1><form action="wordProcess.php" method="post"> 請輸入英文:<input type="text" name="enword" /><br> 請輸入中文:<input type="text" name="chword" /><br> <input type="hidden" name="type" value="add" /> <input type="submit" value="添加">form>

html>

$type=$_POST['type'];$xmlDoc=new DOMDocument();$xmlDoc->load("words.xml");if($type=="query"){ $query_word=$_POST['enword']; $words=$xmlDoc->getElementsByTagName("word"); $isEnter=false; for($i=0;$i<$words->length;$i++){ $word=$words->item($i); $word_en=getNodeVal($word,"en"); if($query_word==$word_en){ $isEnter=true; echo $query_word."--中文意思:".getNodeVal($word,"ch")."
"; } } if(!$isEnter){ echo "NOT FOUNDED"; }}else if($type=="add"){ $eng_word=$_POST['enword']; $ch_word=$_POST['chword']; $root=$xmlDoc->getElementsByTagName("words")->item(0); $new_word=$xmlDoc->createElement("word"); $new_word_en=$xmlDoc->createElement("en"); $new_word_en->nodeValue=$eng_word; $new_word_ch=$xmlDoc->createElement("ch"); $new_word_ch->nodeValue=$ch_word; $new_word->appendChild($new_word_en); $new_word->appendChild($new_word_ch); $root->appendChild($new_word); $b=$xmlDoc->save("words.xml"); if(!$b){ echo "添加失敗"; }else{ echo "添加成功"; }}function getNodeVal(& $mynode, $tagname){ return $mynode->getElementsByTagName( $tagname)->item(0)->nodeValue;}echo "
BACK ";


以上就介紹了PHP:基於XML的線上小詞典案例,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.