2013-07-06 49 views
0

我正在使用簡單的HTML dom來抓取縮放的數據,並且它一直運行良好。然而,我有一個源沒有任何獨特的領域,所以我試圖str_replace,然後抓住我重命名的元素,然後使用simple_html_dom。在simple_html_dom之前使用str_replace

但是,它不起作用。我的代碼是:

require('simple_html_dom.php'); 

// Create DOM from URL or file 
$html = file_get_html('http://www.url.com'); 

$html = str_replace('<strong>','',$html); 

$html = str_replace('</strong>','',$html); 

$html = str_replace('<span class="pound">&pound;</span>','',$html); 

$html = str_replace('<td>','<td class="myclass">',$html); 

foreach($html->find('td.myclass') as $element) 
    $price = $element->innertext; 

$price = preg_replace('/[^(\x20-\x7F)]*/','', $price); 

echo $price; 
+0

你需要做您的字符串替換你的HTML加載到一個DOMDocument之前 – 2013-07-06 20:44:39

回答

0

嘗試

<?php 
    require('simple_html_dom.php'); 
    // Create DOM from URL or file 
    $html = file_get_html('http://www.url.com'); 

    foreach($html->find('td') as $element) { 
    $price = trim(str_replace("&pound;", "", $element->plaintext)); 
    } 

    $price = preg_replace('/[^(\x20-\x7F)]*/','', $price); 

    echo $price; 
?>