duanqi5114 2018-09-02 06:06
浏览 79

仅匹配正则表达式保留大小写的一部分

Lets say I want to replace a word saved in a string. I want to replace that word with the same word, that I have stored with the first letter as lower-case in a variable, adding html tags in the expression. I do care the case sensitivity in all letters but first.

So far: no problem.

But when I replace the expression, it always returns me the first letter with lower-case. And I wanted to replace all of them with their case. Example:

Subject: "<p>Lucky said that lucky is an unlucky word.</p>"

Word stored and searched: "lucky"

What I'd love to have: "<p><b>Lucky</b> said that <b>lucky</b> is an unlucky word.</p>"

That's what I've tried:

$word="lucky";
$result=preg_replace("/\b(?i)".substr($word,0,1)."(?-i)".substr($word,1)."\b/","<b>".$word."</b>","<p>Lucky said that lucky is an unlucky word.</p>");

It returns:

<p><b>lucky</b> said that <b>lucky</b> is an unlucky word.</p>

First lucky does not keep the "L".

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料