在某个字符php之后删除html代码

时间:2016-08-18 18:04:37

标签: php html regex substr

问题:我已将完整的html代码插入到mysql数据库中。我想删除Product Description之后的所有内容。我该怎么做?

<p>hjfsdgfsgdfjsdgfjgdsjfgsdjgfjsdgf </p>
<p>Product Description</p>

我想用html格式再次存储到数据库中。

3 个答案:

答案 0 :(得分:0)

你想要这个吗? http://php.net/manual/en/function.strip-tags.php

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";

// Allow <p> and <a>
echo strip_tags($text, '<p><a>');

答案 1 :(得分:0)

这是一种方法:

$lookup = "Product Description";
$fullText = "..."; //full html code goes here
$pos = strpos($fullText, $lookup);

echo substr($fullText, 0, $pos);

这将打印您的字符串内容,直到“产品描述”位置。如果您还想删除可能的html标记,只需按照其他答案中的建议使用strip_tags

答案 2 :(得分:0)

回答原问题评论中提出的问题,你可以这样做:

preg_replace("/<div class=\"ui-box product-description-main\".*/sm", " ",$your_html_goes_here);

这会删除div元素之后的所有内容,并使用类&#39; ui-box&#39;和&#39; product-description-main&#39;。

出于同样的原因,回答你的原始问题非常简单:

preg_replace("/(<p>Product Description<\/p>).*/sm", "$1", $your_html_goes_here);

这会删除段落&#39;产品说明&#39;之后的所有内容。 如果您需要有关我的答案中提出的正则表达式的更多信息,请告诉我。