dpict99695329 2019-04-02 09:35 采纳率: 100%
浏览 51
已采纳

str_ireplace() - 我可以获取要替换的字符串吗? [重复]

This question already has an answer here:

I have the following strings.

$string = "Welcome to Lorien.";
$search = "welcome";

Now I want to search for $search in $string and ignoring the case, so:

$result = str_ireplace($search,  '<b>'.$search.'</b>' ,$string);

$result should look like this: welcome to Lorien.

Is there a way i can use the case of $string so it looks like this: Welcome to Lorien.

Like getting the part of the string which is getting replaced and use it?

</div>
  • 写回答

2条回答 默认 最新

  • dongpu4141 2019-04-02 09:44
    关注

    You can use preg_replace() for this:

    $string = "Welcome to Lorien.";
    $search = "welcome";
    
    $result = preg_replace("/{$search}/i", '<b>$0</b>', $string);
    
    • The /i-flag makes the matching case-insensitive.
    • The $0 will be replaced with the matched string from the original string so the casing will always be correct.

    Demo: https://3v4l.org/vN9Km

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

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题