求助 php匹配替换多个指定内容里面的字符串

alphapn2000 2011-05-05 11:06:19
要替换的xml文件
<?xml version="1.0" encoding="gb2312" ?> 
<rss version="2.0">
<item>
<title>标题1</title>
<description>
<table>
<tbody>
<tr align="center">
<td><p>内容......</p></td>
</tr>
</tbody>
</table>
</description>
</item>


<item>
<title>标题2</title>
<description>
<table>
<tbody>
<tr align="center">
<td><p>内容......</p></td>
</tr>
</tbody>
</table>
</description>
</item>

</rss>



现在要求替换<description></description>标签里面的html代码的“<”“>”替换为“<”“>”,其他标签的“<”“>”不变,这个php的正则表达式应该怎么写呢

替换的xml文件应该为
<?xml version="1.0" encoding="gb2312" ?> 
<rss version="2.0">
<item>
<title>标题1</title>
<description>
<table>
<tbody>
<tr align="center">
<td><p>内容......</p></td>
</tr>
</tbody>
</table>
</description>
</item>


<item>
<title>标题2</title>
<description>
<table>
<tbody>
<tr align="center">
<td><p>内容......</p></td>
</tr>
</tbody>
</table>
</description>
</item>

</rss>

...全文
422 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2011-05-05
  • 打赏
  • 举报
回复
$s = <<< TEXT
<?xml version="1.0" encoding="gb2312" ?>
<rss version="2.0">
<item>
<title>标题1</title>
<description>
<table>
<tbody>
<tr align="center">
<td><p>内容......</p></td>
</tr>
</tbody>
</table>
</description>
</item>


<item>
<title>标题2</title>
<description>
<table>
<tbody>
<tr align="center">
<td><p>内容......</p></td>
</tr>
</tbody>
</table>
</description>
</item>

</rss>
TEXT;

$p = '/<(description)>(.*?)<\/\\1>/ise';
$r = '"<$1>".htmlspecialchars("$2", ENT_NOQUOTES)."</$1>"';
echo preg_replace($p, $r, $s);
<?xml version="1.0" encoding="gb2312" ?> 
<rss version="2.0">
<item>
<title>标题1</title>
<description>
<table>
<tbody>
<tr align="center">
<td><p>内容......</p></td>
</tr>
</tbody>
</table>
</description>
</item>


<item>
<title>标题2</title>
<description>
<table>
<tbody>
<tr align="center">
<td><p>内容......</p></td>
</tr>
</tbody>
</table>
</description>
</item>

</rss>
alphapn2000 2011-05-05
  • 打赏
  • 举报
回复
谢谢!!!!

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧