php
  • dom
  • 2015-04-02 53 views 1 likes 
    1

    我想獲得與div的價值與內部大教堂通過ID獲取元素,當我嘗試這樣做,我得到的價值是'遊戲和新遊戲發佈'從該div和我沒有得到的PHP值和PHP標籤在所有例如。 <? php回聲hellow世界如下所示。我想獲得變量$ existing_Data中的div的所有內容,包括<? PHP和< H1>等。獲取php標籤內的一個div與PHP大教堂

    $selected_div = "div_footer"; 
    $file = '../myfolder/22selected.php'; 
    $file_get = file_get_contents($file); 
    
    /* // which one to use here ? i am getting a headache ! 
    $file_entity = htmlentities($file_get); 
    $file_htmlcharac = htmlspecialchars($file_entity); 
    $file_strip = stripslashes($file_htmlcharac); 
    */ 
    
    $doc = new DOMdocument(); 
    $doc->loadHTML($file_get); 
    $element = $doc->getElementById($selected_div); 
    $existing_Data = $element->firstChild->nodeValue; 
    
    echo '<table>'; 
    echo '<form action= "existing_Data.php" method = "POST">'; 
    echo 'Text Content <textarea rows="12" cols="60" name="content" id ="text_content">' . $existing_Data . '</textarea>'; 
    echo '<input type ="submit" name = "submit" id = "submit" value = "Submit" ></form>'; 
    
    // div_footer CONTENTS 
    <div id = "div_footer"> 
    <h1> All games</h1> 
    <p> new games releases</p> 
    <?php echo "hello world"; ?> 
    </div> 
    
    +0

    什麼你想做什麼? – 2015-04-02 04:27:54

    回答

    1

    從大家展示一下,我們無法猜測你想要達到的目標。

    當您在沒有方案(http,ftp,..)的情況下使用file_get_contents()打開文件時,PHP會嘗試通過本地文件系統打開該文件,因此標籤不會被PHP解釋。缺點是,它不是有效的XML/HTML,所以一個DOM解析器將無濟於事。

    您應該將數據寫入數據庫或至少一個XML,JSON或純文本文件。爲了獲得價值,你需要做的是$value = trim(file_get_contents('../mydata.txt'));

    另一種情況是,您想要(http-)POST該數據。那麼也沒有理由使用DOM解析器。只需將數據放入隱藏字段<input type="hidden" name="data" value="<?php echo $data; ?>即可。

    爲了防止不同的(不是全部)類型的XSS,請參閱

    htmlentities() vs. htmlspecialchars()

    How to prevent XSS with HTML/PHP?

    我強烈建議閱讀本QA:

    What are the differences between server-side and client-side programming?

    +0

    嘗試此操作,更改'$ file ='../ myfolder/22selected.php';'並添加您的http url'$ file ='http://yoursite.com/myfolder/22selected.php';' – DanFromGermany 2015-04-02 05:28:36

    +0

    hey bro ,那些建議已經完成了50%的工作,現在我可以看到PHP代碼中的值,例如hello world,但我仍然無法看到標籤,例如<? PHP中,我想將$ existing_data中的值從這裏取到另一個表單,並將其寫入其中,這在此處未顯示,因此$ existing_Data應該包含div_footer的全部內容,例如..只是不是Hello world,而是<? PHP回聲你好世界?>< - 與標籤 – Manu 2015-04-02 05:41:30

    +0

    感謝兄弟。得到它排序,你的男人! – Manu 2015-04-02 05:53:38

    相關問題