douaikuai2715 2014-04-17 10:29
浏览 39
已采纳

检查字符串以查看它是否包含数组中的一个字符串

Been scratching my head all morning and cannot seem to think of a logical quick way of doing this, without using alot of resources.

Here is the scenario,

$content = "hello this is a lovely website that people help me with and i love it";
$arrayto = array("good morning","hello","good afternoon","morrow");
$website = "http://www.google.com";

I would like to check the $content and if it contains one of the arrays words and if it does turn it into a link using the $website as the href="", and then stop as soon as it finds one.

So then $content would be "<a href="$website">hello</a> this is a lovely website that people help me with and i love it";.

Thanks

  • 写回答

5条回答 默认 最新

  • dongna9185 2014-04-17 10:43
    关注

    You can do without Regex using str_replace

     <?php
        $content = "hello this is a lovely website that people help me with and i love it";
        $arrayto = array("good morning","hello","good afternoon","morrow");
        $website = "http://www.google.com";
    
        foreach($arrayto as $v){
         if(strpos($content,$v)!==false){
       $content= str_replace("$v","<a href=$website>$v</a>",$content);
            }
        }
        echo $content;
    

    <kbd>DEMO</kbd>

    OUTPUT:

    <a href=http://www.google.com>hello</a> this is a lovely website that people help me with and i love it
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器