我很难弄清楚怎么做,我有一个字符串看起来像这样…
$text = "<p>This is some example text This is some example text This is some example text</p>
<p><em>This is some example text This is some example text This is some example text</em></p>
<p>This is some example text This is some example text This is some example text</p>";我基本上想使用像preg_repalce和正则表达式这样的东西来删除
<em>This is some example text This is some example text This is some example text</em>所以我需要编写一些PHP代码来搜索打开的<em>和关闭的</em>,并删除其间的所有文本
希望有人能帮忙,谢谢。
发布于 2011-09-23 21:42:18
如果您对非正则表达式解决方案感兴趣,也可以执行以下操作:
<?php
$text = "<p>This is some example text This is some example text This is some example text</p>
<p><em>This is some example text This is some example text This is some example text</em></p>
<p>This is some example text This is some example text This is some example text</p>";
$emStartPos = strpos($text,"<em>");
$emEndPos = strpos($text,"</em>");
if ($emStartPos && $emEndPos) {
$emEndPos += 5; //remove <em> tag aswell
$len = $emEndPos - $emStartPos;
$text = substr_replace($text, '', $emStartPos, $len);
}
?>这将删除标签之间的所有内容。
发布于 2011-09-23 21:36:36
$text = preg_replace('/([\s\S]*)(<em>)([\s\S]*)(</em>)([\s\S]*)/', '$1$5', $text);发布于 2011-09-23 21:41:07
使用strrpos查找第一个元素,然后查找最后一个元素。使用substr获取字符串的一部分。然后用原始字符串中的空字符串替换该子字符串。
https://stackoverflow.com/questions/7529636
复制相似问题