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

时间:2021-06-21 01:34:52

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.

我想检查$ content,如果它包含一个数组单词,如果它确实将它变为使用$ website作为href =“”的链接,然后在找到它时立即停止。

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

那么$ content将是“你好这是一个可爱的网站,人们帮助我,我喜欢它”;

Thanks

谢谢

5 个解决方案

#1


0  

You can do without Regex using str_replace

你可以使用str_replace而不使用Regex

 <?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

DEMO

OUTPUT:

OUTPUT:

<a href=http://www.google.com>hello</a> this is a lovely website that people help me with and i love it

#2


1  

Source link str_replace could be an answer

源链接str_replace可能是一个答案

// 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);

#3


0  

$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.

您可以使用函数strstr。

#4


0  

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

OUTPUT-你好,这是一个可爱的网站,人们帮助我,我喜欢它

#5


0  

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

这可以通过有效的方式解决,而无需使用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.

请注意,如果您只想替换整个单词,则必须使用正则表达式和\ b位,否则会将helloween转换为 hello ween。

#1


0  

You can do without Regex using str_replace

你可以使用str_replace而不使用Regex

 <?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

DEMO

OUTPUT:

OUTPUT:

<a href=http://www.google.com>hello</a> this is a lovely website that people help me with and i love it

#2


1  

Source link str_replace could be an answer

源链接str_replace可能是一个答案

// 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);

#3


0  

$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.

您可以使用函数strstr。

#4


0  

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

OUTPUT-你好,这是一个可爱的网站,人们帮助我,我喜欢它

#5


0  

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

这可以通过有效的方式解决,而无需使用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.

请注意,如果您只想替换整个单词,则必须使用正则表达式和\ b位,否则会将helloween转换为 hello ween。