首页 文章

PHP和mySQL - 将单个HTML字段文本解析/分离为2个变量

提问于
浏览
2

提前感谢您抽出宝贵时间解决我的问题 .

我正在使用PHP脚本来查询mySQL数据库 . 不幸的是,产品描述,成分和附加信息(最后的段落)都存储在一个字段中,同时还有名称(名称是多余的,可以忽略) . 所有文本都包含在HTML代码中 . 我不想保留或存储任何HTML代码,但它可能有用作分隔符 .

Important: HTML是以编码方式存储的,所以

<p>

存储为

&lt;p&gt;

下面是一个存储在mySQL数据库中的HTML代码示例(这是它存储的确切方式 . 正如我之前提到的,HTML是经过编码的 . ):

&lt;table border=&quot;0&quot; cellpadding=&quot;2&quot; cellspacing=&quot;2&quot;&gt;
&lt;tbody&gt;
    &lt;tr valign=&quot;top&quot;&gt;
        &lt;td&gt;
            Item:&lt;/td&gt;
        &lt;td&gt;
            Olive Loaf - Baked - &lt;b&gt;Gluten Free!&lt;/b&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr valign=&quot;top&quot;&gt;
        &lt;td&gt;
            Description:&lt;/td&gt;
        &lt;td&gt;
            A blend of beef and pork along with our unique spices to create a base mix. To this mix we add plenty of olives and form it into a loaf, we then smoke this over natural hardwoods for a unique Koegel flavor.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr valign=&quot;top&quot;&gt;
        &lt;td&gt;
            Ingredients:&lt;/td&gt;
        &lt;td&gt;
            Beef and Pork, Water, Spanish Olives (Olives, Pimentos, Sodium Alginate, Guar Gum, Calcium Chloride, Water, Salt, Lactic Acid), Nonfat Dry Milk, Corn Syrup, Salt, Red Sweet Peppers (bell peppers, water, citric acid.), Spices, Dextrose, Dehydrated Onions.&lt;/td&gt;
    &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
    &lt;strong&gt;Each loaf weighs approximately 6 lbs.&lt;/strong&gt;&lt;/p&gt;

这是与HTML解码相同的代码片段(这不是它如何存储在mySQL数据库中 . 我提供这只是为了视觉 . ):

<table border="0" cellpadding="2" cellspacing="2">
<tbody>
    <tr valign="top">
        <td>
            Item:</td>
        <td>
            Olive Loaf - Baked - <b>Gluten Free!</b></td>
    </tr>
    <tr valign="top">
        <td>
            Description:</td>
        <td>
            A blend of beef and pork along with our unique spices to create a base mix. To this mix we add plenty of olives and form it into a loaf, we then smoke this over natural hardwoods for a unique Koegel flavor.</td>
    </tr>
    <tr valign="top">
        <td>
            Ingredients:</td>
        <td>
            Beef and Pork, Water, Spanish Olives (Olives, Pimentos, Sodium Alginate, Guar Gum, Calcium Chloride, Water, Salt, Lactic Acid), Nonfat Dry Milk, Corn Syrup, Salt, Red Sweet Peppers (bell peppers, water, citric acid.), Spices, Dextrose, Dehydrated Onions.</td>
    </tr>
</tbody>
</table>
<p>
    <strong>Each loaf weighs approximately 6 lbs.</strong></p>

基本上,我想忽略名称,将描述和附加信息(描述下面的最后一段)保存为$ productDescription(可能在描述后添加两个换行符以分隔附加信息),并将成分保存为$ productIngredients . 我想 not 想要包含文字"Description:"或"Ingredients:" ......只是紧接着之后的信息 . As stated above, I am only interested in the raw text - I do not want to save any of the HTML code. All HTML code should be ignored when storing the information into the 2 variables.

任何帮助都很受欢迎!

谢谢,

-Jeff

EDIT

发生了什么是mySQL数据库连接到opencart网站 . 在网站的管理员方面,没有单独的成分,描述等字段 . 其他一切(重量,尺寸,SKU,型号等) . 这是因为opencart网站并非专门用于食品 . 它可以用于电子设备,在这种情况下不需要这样的领域 . 商店所有者(不是我)将所有这些信息提供给描述入口点 . HTML可能会放弃这一点 . 该网站由另一个人管理(如果需要,我可以直接与他联系) . 我宁愿不让他或我自己改变opencart代码来添加额外的字段,除非它可以很容易地完成 .

将其他字段添加到opencart网站会很容易吗?这样,描述字段可以是排他性的 . 谢谢你的回复 .

1 回答

  • 1

    这个问题很简单 . 我认为做这个设计的人是一个错误 . 他只是在数据库字段中存储了一段授权HTML . 现在,您正尝试从中提取特定信息 .

    显然,你在检索其中一个项目时所做的第一件事就是取消它的权利(将 &lt; 转入 < 等) . html_entity_decode这样做 .

    现在它看起来像你的html格式正确 . 也就是说, <p></p> 元素等正确匹配 . 这很好,因为您可以使用XML库来操作它 .

    这是来自我的项目的示例代码 . 我没有尝试在你的项目中调试它 .

    $xml= simplexml_load_string("<?xml version='1.0'?>\n" . html_entity_decode($mydata);
     $ns = $xml->getNamespaces(true);
     foreach ($ns as $key => $val) {
        $xml->registerXPathNamespace($key, $val);
     }
     unset($ns);
    

    现在你有一个包含你的小文章的simpleXML对象 . 您可以使用各种API来提取所需的数据 . 看看这里的一些例子 .

    http://php.net/manual/en/simplexml.examples-basic.php

    我怀疑你能够很容易地使用这种材料 .

    请注意,在评论中有些人说过,您应该尝试从这些HTML节中提取有趣的信息并将其放入专用列中 . 这可能是真的,特别是如果你必须搜索这些数据或动态更新它 .

    但是也可以将内容存储在XML / HTML中 . 如果需要搜索,可以使用FULLTEXT搜索 .

    编辑

    您可能需要将XML内容包装在单个 <article> ... </article> 节中,就像这样 . 最后一项,即关于面包重量的段落,可能会被拒绝作为额外的 .

    $xml= simplexml_load_string("<?xml version='1.0'?>\n<article>\n" .
        html_entity_decode($mydata) . "\n</article>\n";
    

    处理XML需要一些愚弄,至少对我而言 .

相关问题