You can use PHP's built-in functions. strip_tags() Use a regular expression to remove `div` tags from HTML code. Here is an example code:
$html = '<div>这是一个 div 标签</div><p>这是一个 p 标签</p>';
// 去除所有 div 标签及其内容
$html = preg_replace('/<div\b[^>]*>(.*?)<\/div>/is', '', $html);
// 去除所有标签,只保留文本内容
$html = strip_tags($html);
echo $html; // 输出:这是一个 p 标签In this example, it is used first. preg_replace() Functions and Regular Expressions /<div\b[^>]*>(.*?)<\/div>/is Removes all <div> tags and their content. This regular expression matches: <div> Replace the tags and their content with an empty string.
Then, use it. strip_tags() The function removes all tags, retaining only the text content. The final output is the text content with the <div> tags removed.
Please note that this regular expression does not fully match all possible `div` tags; for example, it may not correctly handle `div` tags with attributes. If your HTML code contains more complex `div` tag structures, you may need to use a more sophisticated regular expression to match and remove these tags.