drgdn82648 2017-10-02 19:07
浏览 49
已采纳

在另一个字符串中查找并着色字符串php [关闭]

I have a String, let's say:

$needle = "HELLO";

And I have another String I want to find $needle in, let's say:

$haystack = "HELLO WORLD";

I know there are methods which return the position as an int etc. but how could I then color that very string when it's being output? I made myself an array with some chars in it which are being output randomly like 2000 times and I'd like to see them in color when they make up the right word.

  • 写回答

1条回答 默认 最新

  • douyingzhan5905 2017-10-02 19:22
    关注

    I've interpreted your question as:

    I want to find a string within another string, making the matching characters a different colour in the output

    Easiest method would be

    <style>.myclass { color: red; }</style>
    <?php
    
    $needle = "HELLO";
    $haystack = "HELLO WORLD";
    
    $myOutput = str_replace($needle,"<a class='myclass'>".$needle."</a>",$haystack);
    
    echo $myOutput;
    
    ?>
    

    Obviously editing it to suit the method you're intending for it, but the above should help with one possible solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集