douwei1408 2012-09-05 19:20
浏览 25
已采纳

PHP preg_replace&符号

Okay I might not be going about this the right way, but here goes..

I have this string that takes a link and extracts the text between the tags...

$string = $item;
$pattern = '/\<a([^>]*)\>([^<]*)\<\/a\>/i';
$replacement = '$2';
$message = preg_replace($pattern, $replacement, $string);

There are a few items in this string that have ampersands (in the text portion, not the tag portion), however most don't. I'm trying to figure out a way to either incorporate the ampersand into the current pattern or do another preg_replace on the $message to remove the ampersand after the tags are striped away.

THANKS!

  • 写回答

2条回答 默认 最新

  • douyan2470 2012-09-05 19:25
    关注

    Do you want to remove everything after the ampersand? Then it's

    '/\<a([^>]*)\>([^<&]*)[^<]*\<\/a\>/i';
    

    Otherwise, you'll need a 2nd operation.

    BTW: Your regex will also match other tags starting with <a, such as the <author> or the <audio> tag.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题