douxianji3367 2017-09-04 15:17
浏览 41
已采纳

PHP:如何在不删除太多字符的情况下删除2个字符串之间的字符串部分?

This problem is driving me nuts. Let's say I have a string:

This is a &start;pretty bad&end; string that I want to &start;somehow&end; display differently

I want to be able to remove the &start; and &end; parts as well as everything in between so it says:

This is a  string that I want to  display differently

I tried using preg_replace with a regular expression but it took off too much, ie:

This is a  display differently

The question is: how do I remove the stuff just between sets of &start; and &end; pairs and make sure that it doesn't remove anything between any &end; and &start; segments?

Keep in mind, I'm working with hundreds of strings that are very different to each other so I'm looking for a flexible solution that'll work with all of them.

Thanks in advance for any help with this.

Edit: Replaced dollar signs with ampersands. Oops!

  • 写回答

2条回答 默认 最新

  • drhg24275 2017-09-04 15:29
    关注

    Try this regex /\&start;(.+?)\$end;/g

    It looks like it works as desired: https://regex101.com/r/MW5nom/2

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?