2010-05-16 82 views
2

嗨,大家好我正在使用這個美妙的類在這裏做一些代碼嵌入過濾:http://simplehtmldom.sourceforge.net/。它擴展了PHP DOM文檔類。PHP簡單的DOM解析器

我所做的大部分工作是通過這個包含嵌入代碼的類來解析字符串,我通過一個處理函數來獲取信息的獨特位,例如id,width,height等。我預定義的「安全」模板,並將我的安全模板重新插入用戶放入的嵌入代碼的位置。可能看起來是一種倒退的方式,但它是它必須完成的方式:)

所有這一切工作正常。問題是,當字符串中包含的不僅僅是嵌入代碼時,因爲我不能只替換嵌入代碼,我只能替換整個字符串來擦除其餘的標籤等字符串。例如,如果有一個p標籤將被擦除。

所以我的問題是如何使用這個類,我可以只取代字符串的某些部分?花了最近幾天試圖解決這個問題,需要更多的輸入。看起來這個班可以做到這一點,所以我很難過。

這裏是什麼,我至今基本版本:)

// load the class 
    $html = new simple_html_dom(); 

    // load the entire string containing everything user entered here 
    $return = $html->load($string); 

    // check for embed tags 
    if($html->find('embed') == true 
    { 
     foreach($html->find('embed') as $element) 
     { 
       // send it off to the function which returns a new safe embed code 
       $element = create_new_embed($parameters); 

       // this is where i somehow i need to save the changes and send it back to $return 
     } 
    } 

任何投入將感激地讚賞。如果我已經解釋了我的問題不夠好,請讓我知道:)

回答

4

當你這樣做:

foreach($html->find('embed') as $element) 

你正在創建一個simpleHTMLdom元素對象 - 它不是一個簡單的變量,你可以只使用作爲分配。對於〔實施例,如果該元素是:

<embed>Some Embed Code Here</embed> 

你可以這樣做改變了下列文件:

$element->innertext = "Hello world"; 
# the element is now <embed>Hello world</embed> 
如果你想完全改變元素的

$element->outertext = "<p>Now I'm a paragraph</p>"; 

和你已經完全取代了嵌入。

+0

當然,geez很明顯。非常感謝你。 – 2010-05-16 05:17:01

+0

謝謝您以前的快速回復! – 2010-05-16 05:37:52

+1

沒問題。如果這回答你的問題,請考慮點擊好的複選標記,這意味着......它最好的回答你的問題:-)歡迎來到Stackoverflow。 – Erik 2010-05-16 05:45:52

相關問題