0

我有一个奇怪的场景,我就是无法理解

从 PHP 中的字符串中删除的标签。

这就是发生的事情,我正在使用编码 ASCII 从数据库中提取数据

字符串看起来像这样

<p>Blue Power Waterproof</p> 

现在我执行以下操作来解码实体

html_entity_decode($p->description)

以下结果<p>Blue Power Waterproof</p>我需要删除

标签,但以下不起作用

strip_tags(html_entity_decode($p->description))

removeParagraphTags(html_entity_decode($p->description);

function removeParagraphTags($html){ 
            $pattern = "'#<p[^>]*>(\s|&nbsp;?)*</p>#'"; 
            iconv(mb_detect_encoding($html, "auto"), 'UTF-8', $html);
            return preg_replace($pattern, '', $html); 
        }
4

1 回答 1

1

我通过从与 HTML 实体一起存储的数据库中获取所有描述来解决这个问题,解码实体以便将实体转换为 html,然后用 html 而不是实体更新每条记录,这解决了问题

$sql = "SELECT * FROM products";
$result=  pg_query($conn,$sql);
while ($row = pg_fetch_array($result,null,PGSQL_ASSOC)) {
    $name = html_entity_decode($row['name']);
    $name = iconv('UTF-8', 'ASCII', $name);
    $desc = html_entity_decode($row['description']);
    $desc = iconv('UTF-8', 'ASCII', $desc);
    $ldesc = html_entity_decode($row['longDescription']);
    $ldesc = iconv('UTF-8', 'ASCII', $ldesc);
    $up = "UPDATE products SET name='".pg_escape_string($name)."',description='".  pg_escape_string($desc)."',\"longDescription\"='".pg_escape_string($ldesc)."' WHERE p_id=".$row['p_id'];

    pg_query($conn,$up) OR die(pg_last_error());
}
于 2013-02-19T12:07:06.937 回答