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

时间:2016-10-26 13:05:17

标签: php mysql extract

您好我想要提取文本并将它们插入到数据库表中 这是产品描述代码

> <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">中我将输入数据库的值感兴趣

我为html制作了<br><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));
        }

由于

1 个答案:

答案 0 :(得分:0)

您可以使用简单的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>";
}