我有个奇怪的场景,我就是搞不懂
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| ?)*</p>#'";
iconv(mb_detect_encoding($html, "auto"), 'UTF-8', $html);
return preg_replace($pattern, '', $html);
}发布于 2013-02-19 20:07:07
我解决了这个问题,方法是从数据库中获取以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());
}https://stackoverflow.com/questions/14853213
复制相似问题