开发者

Matching words in strings [duplicate]

开发者 https://www.devze.com 2023-04-01 22:56 出处:网络
This question already has answers here: Php compare strings and return common values (3 answers) Closed 8 years ago.
This question already has answers here: Php compare strings and return common values (3 answers) Closed 8 years ago.

I have two strings of keyword开发者_StackOverflows

$keystring1 = "tech,php,radio,love";
$keystring2 = "Mtn,huntung,php,tv,tech";

How do i do return keywords that common in both strings


You can do this:

$common = array_intersect(explode(",", $keystring1), explode(",", $keystring2));

If you want them back into strings, you can just implode it back.


Hmm, interesting question... You can use this.

$arr1 = explode(',',$keystring1);
$arr2 = explode(',',$keystring2);
$duplicates = array_intersect($arr1,$arr2);
foreach($duplicates as $word) {
    echo $word;
}


You could explode() both strings on commas into arrays and loop through the first array checking to see if any of the words exist in the second array using the in_array() function. If so then add that word to a "common words" array.


Those are going to need to be arrays not variables.

$keystring1 = array('tech','php','radio','love');
$keystring2 = array('mtn','huntung','php','tv','tech');

First of all...

0

精彩评论

暂无评论...
验证码 换一张
取 消