donglanfu5831 2018-08-11 09:09
浏览 41

在特定句子之间插入<span>标签,保留原始案例

I need to insert a span tag between a certain text from a string, but it should be case insensitive and keep the original case.

$string ="This is what this is";

echo str_replace("this is","[span]this is[/span]",$string);

Returns:

This is what [span]this is[/span]

Expected:

[/span]This is[/span] what [span]this is[/span]

I know the str_replace is not the good option for that, and ir might be probably handled by Regex, but I have no idea in which direction I should go. Thanks in advance for your help!

  • 写回答

1条回答 默认 最新

  • douzhang7603 2018-08-11 09:14
    关注

    Use preg_replace:

    $string ="This is what this is";
    
    echo preg_replace("/this is/i","[span]$0[/span]",$string);
    

    Explanation:

    /           : regex delimiter
       this is  : literally
    /i          : regex delimiter, case insensitive
    

    Replacement:

    $0    : contains the whole match, ie "this is"
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题