删除某个字符 php 后的 html 代码

remove html code after a certain character php

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

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

我想以html格式再次存入数据库。

你想要这个吗?
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>');

这是一种方法:

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

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

这将打印您的字符串内容,直到 "Product Description" 位置。如果您还想摆脱可能的 html 标签,只需按照其他答案

中的建议使用 strip_tags

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

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

这将删除 div 元素之后的所有内容 类 'ui-box' 和 'product-description-main'。

同理,回答你原来的问题也很简单:

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

这将删除段落 'Product Description' 之后的所有内容。 如果您需要有关我的回答中提出的正则表达式的更多信息,请告诉我。