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条)

报告相同问题?