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

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

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;
    

    DEMO

    OUTPUT:

    <a href=http://www.google.com>hello</a> this is a lovely website that people help me with and i love it
    
    已采纳该答案
    打赏 评论
  • douyong1908 2014-04-17 10:40

    Source link str_replace could be an answer

    // Provides: You should eat pizza, beer, and ice cream every day
        $phrase  = "You should eat fruits, vegetables, and fiber every day.";
        $healthy = array("fruits", "vegetables", "fiber");
        $yummy   = array("pizza", "beer", "ice cream");
    
        $newphrase = str_replace($healthy, $yummy, $phrase);
    
    打赏 评论
  • doujun7161 2014-04-17 10:42

    This will do it-

    <?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";
    
    $data=explode(' ', $content);
    $final=array();
    
    foreach ($data as $key ) {
    
        if(array_search($key, $arrayto))
        {
            $word='<a href='.$website.'>'.$key."</a>";
            array_push($final, $word);
        }
        else
        {
          array_push($final, $key);
        }
    }
    
    $res=implode(' ', $final);
    
    print_r($res);
    
    ?>
    

    OUTPUT- hello this is a lovely website that people help me with and i love it

    打赏 评论
  • duankuixi1930 2014-04-17 10:47
    $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";
    while($item = each($arrayto)){
        if(strstr($content, $item['value'])!==false){
            $content = str_replace($item['value'], '<a href="'.$website.'">'.$item['value'].'</a>', $content);
            break;
        }
    }
    

    You can use function strstr.

    打赏 评论
  • dpjjmo3079 2014-04-17 10:48

    This can be solved in an efficient way without looping using preg_replace:

    $content = "hi hello and bye";
    $words = array('hello', 'bye');
    $website = "http://www.google.com";
    
    $regex = '/\b(' . implode('|', $words) . ')\b/';
    echo preg_replace($regex, "<a href='$website'>$1</a>", $content, 1);
    

    Note that regexes and \b bits are necessary if you want to replace whole words only, otherwise it turns helloween into <a...>hello</a>ween.

    打赏 评论

相关推荐 更多相似问题