<div id="productDetails" class="tabContent active details">
<span>
<b>Case Size:</b>
</span>
44mm
<br>
<span>
<b>Case Thickness:</b>
</span>
13mm
<br>
<span>
<b>Water Resistant:</b>
</span>
5 ATM
<br>
<span>
<b>Brand:</b>
</span>
Fossil
<br>
<span>
<b>Warranty:</b>
</span>
11-year limited
<br>
<span>
<b>Origin:</b>
</span>
Imported
<br>
</div>
如何通过PHP中的DOM解析器获取44mm,化石等数据?
我可以轻松获得的数据
$data=$html->find('div#productDetails',0)->innertext;
var_dump($data);
但是我想在我的sql表的meta_key和meta_value中打破它... 我可以通过
获得meta_key
$meta_key=$html->find('div#productDetails span',0)->innertext;
但与之相关的元值????
答案 0 :(得分:1)
这并不难,真的... just google, and click this link,您现在知道如何解析DOM,here您可以看到可以使用哪些方法来选择所有感兴趣的元素,迭代DOM ,得到它的内容,你有什么......
$DOM = new DOMDocument();
$DOM->loadHTML($htmlString);
$spans = $DOM->getElementsByTagName('span');
for ($i=0, $j = count($spans); $i < $j; $i++)
{
echo $spans[$i]->childNodes[0]->nodeValue.' - '.$spans[$i]->parentNode->nodeValue."\n";
}
如果我没弄错的话,这似乎就是你所追求的。这只是我的头脑,但我认为这应该输出如下:
Case Size: - 44mm
Case Thickness: - 13mm
<强>更新强>
如果我没有弄错的话,这是一个经过测试的解决方案,它会返回所需的结果:
$str = "<div id='productDetails' class='tabContent active details'>
<span>
<b>Case Size:</b>
</span>
44mm
<br>
<span>
<b>Case Thickness:</b>
</span>
13mm
<br>
<span>
<b>Water Resistant:</b>
</span>
5 ATM
<br>
<span>
<b>Brand:</b>
</span>
Fossil
<br>
<span>
<b>Warranty:</b>
</span>
11-year limited
<br>
<span>
<b>Origin:</b>
</span>
Imported
<br>
</div>";
$DOM = new DOMDocument();
$DOM->loadHTML($str);
$txt = implode('',explode("\n",$DOM->textContent));
preg_match_all('/([a-z0-9].*?\:).*?([0-9a-z]+)/im',$txt,$matches);
//or if you don't want to include the colon in your match:
preg_match_all('/([a-z0-9][^:]*).*?([0-9a-z]+)/im',$txt,$matches);
for($i = 0, $j = count($matches[1]);$i<$j;$i++)
{
$matches[1][$i] = preg_replace('/\s+/',' ',$matches[1][$i]);
$matches[2][$i] = preg_replace('/\s+/',' ',$matches[2][$i]);
}
$result = array_combine($matches[1],$matches[2]);
var_dump($result);
//result:
array(6) {
["Case Size:"]=> "44mm"
["Case Thickness:"]=> "13mm"
["Water Resistant:"]=> "5"
["ATM Brand:"]=> "Fossil"
["Warranty:"]=> "11"
["year limited Origin:"]=> "Imported"
}
将其插入您的数据库:
foreach($result as $key => $value)
{
$stmt = $pdo->prepare('INSERT INTO your_db.your_table (meta_key, meta_value) VALUES (:key, :value)');
$stmt->execute(array('key' => $key, 'value' => $value);
}
修改
要完全捕获11-year limit
子字符串,您需要编辑上面的代码,如下所示:
//replace $txt = implode('',explode("\n",$DOM->textContent));etc... by:
$txt = $DOM->textContent;//leave line-feeds
preg_match_all('/([a-z0-9][^:]*)[^a-z0-9]*([a-z0-9][^\n]+)/im',$txt,$matches);
for($i = 0, $j = count($matches[1]);$i<$j;$i++)
{
$matches[1][$i] = preg_replace('/\s+/',' ',$matches[1][$i]);
$matches[2][$i] = preg_replace('/\s+/',' ',$matches[2][$i]);
}
$matches[2] = array_map('trim',$matches[2]);//remove trailing spaces
$result = array_combine($matches[1],$matches[2]);
var_dump($result);
输出结果为:
array(6) {
["Case Size"]=> "44mm"
["Case Thickness"]=> "13mm"
["Water Resistant"]=> "5 ATM"
["Brand"]=> "Fossil"
["Warranty"]=> "11-year limited"
["Origin"]=> "Imported"
}
答案 1 :(得分:0)
您可以使用set_callback Api
删除span标记试试这个
$url = "";
$html = new simple_html_dom();
$html->load_file($url);
$html->set_callback('my_callback');
$elem = $html->find('div[id=productDetails]');
$product_details = array();
$attrib = array( 1 => 'size', 2 => 'thickness', 3 => 'wr', 4 => 'brand', 5 => 'warranty', 6 => 'orgin' );
$attrib_string = strip_tags($elem[0]->innertext);
$attrib_arr = explode(' ',$attrib_string); // hope this can help you for every product
// Remove Empty Values
$attrib_arr = array_filter($attrib_arr);
$i = 1;
foreach($attrib_arr as $temp)
{
$product_details[$attrib[$i]] = $temp;
$i++;
}
print_r($product_details);
// remove span tag inside div
function my_callback($element) {
if($element->tag == 'span'){ $element->outertext = ""; }
}