Cod produs (Part number) > original5014702023309 cl-6ren">
gpt4 book ai didi

php - 使用 php mysql 从 html 表中提取值(文本)

转载 作者:行者123 更新时间:2023-11-29 20:08:18 24 4
gpt4 key购买 nike

您好,我想提取文本并将其插入数据库表中这是产品描述的代码

> <table class="AttrDTable"><!--@HeaderPlacehoder--><tr
> class="AttrDRow"><td class="AttrDTypeName">Cod produs (Part number)
> original</td><td class="AttrDValue">5014702023309</td></tr><tr
> class="AttrDAltRow"><td class="AttrDTypeName">Nume produs</td><td
> class="AttrDValue">Pricing gun, single row (8 numeric characters),
> black</td></tr><tr class="AttrDRow"><td
> class="AttrDTypeName">Producator</td><td class="AttrDValue">Avery
> Zweckform</td></tr><tr class="AttrDAltRow"><td
> class="AttrDTypeName">Clasa produsului</td><td
> class="AttrDValue">Aparatură pentru
> birou</td></tr></td></tr><!--@FooterPlacehoder--></table>

我只对 <td class="AttrDTypeName"> 中的值感兴趣和<td class="AttrDValue">我将把这些值输入数据库

我用 <br> 制作了 html和<strong>值这是代码

$rand=explode("&lt;br&gt;",$descriere);
for ($i=0;$i<count($rand);$i++)
if ($rand[$i]!='')
{
$element=explode(": &lt;/strong&gt;",$rand[$i]);
$element[0]=str_replace("&lt;strong&gt;","",$element[0]);
$element[1]=str_replace("&nbsp;","",$element[1]);

echo $element[0]."---".$element[1]."##"."<br>";


$exista=mysqli_query($conexiune, "SELECT * FROM oc_attribute_description WHERE name='".$element[0]."'") or die(mysqli_error($conexiune));
if (!mysqli_num_rows($exista))
{
mysqli_query($conexiune, "INSERT INTO oc_attribute (attribute_group_id) VALUES ('4')");
$id_attr=mysqli_insert_id($conexiune);
//inserez descriere atribut nou in tabela
mysqli_query($conexiune, "INSERT INTO oc_attribute_description (attribute_id, language_id, name) VALUES ('".$id_attr."', '2', '".$element[0]."')") or die(mysqli_error($conexiune));

}
$atribut=mysqli_fetch_array(mysqli_query($conexiune, "SELECT attribute_id FROM oc_attribute_description WHERE name='".$element[0]."'"));

$exista2=mysqli_query($conexiune, "SELECT * FROM oc_product_attribute WHERE product_id='".$product_id."' AND attribute_id='".$atribut[0]."'") or die(mysqli_error($conexiune));
if (!mysqli_num_rows($exista2))
{
mysqli_query($conexiune, "INSERT INTO oc_product_attribute (product_id, attribute_id, language_id, text) VALUES ('".$product_id."', '".$atribut[0]."', '2', '".$element[1]."')") or die(mysqli_error($conexiune));

}
else
mysqli_query($conexiune, "UPDATE oc_product_attribute SET text='".$element[1]."' WHERE product_id='".$product_id."' AND attribute_id='".$atribut[0]."'") or die(mysqli_error($conexiune));
}

谢谢

最佳答案

您可以使用简单的 html dom。它是最简单、最快的 php dom 提取器。我在这里根据您的要求编写一些代码。我假设您的 html 包含数据位于 uss.html 中。所以,这是一个简单的代码

include 'simple_html_dom.php';
$html = new simple_html_dom();
$html = file_get_html("uss.html");
$data = $html->find('td[class=AttrDTypeName]');
$data1 = $html->find('td[class=AttrDValue]');

echo "Your data from, td class="AttrDTypeName"<br><br>";

foreach($data as $val)
{
echo $val->innertext."<br><br>";
}

echo "Your data from, td class="AttrDValue"<br><br>"

foreach($data1 as $val1)
{
echo $val1->innertext."<br><br>";
}

关于php - 使用 php mysql 从 html 表中提取值(文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40263109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com