php比较关键字字符串

发布于 2024-11-19 09:29:59 字数 273 浏览 4 评论 0原文

我需要将这两个关键字字符串与 php.ini 进行比较。

$original_keywords = cat, dog, mouse

$possible_keywords_1 = "cat, dog, hamster";       
$possible_keywords_2 = "cat, sheep, goat"; 

我需要一个 php 脚本将数值“1”分配给第一个可能的字符串(1 个匹配),将“2”分配给第二个可能的字符串(2 个匹配)。有人可以帮我吗?

I need to compare these two strings of keywords with php.

$original_keywords = cat, dog, mouse

$possible_keywords_1 = "cat, dog, hamster";       
$possible_keywords_2 = "cat, sheep, goat"; 

I need the a php script to assign numerical values of "1" to the first possible string (1 match) and "2" to the second (2 matches). Could anyone help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥醉 2024-11-26 09:30:00
$original_keywords = array("cat", "dog", "mouse");
$possible_keywords1 = preg_split('/,\s*/', "cat, dog, hamster");
echo count(array_intersect($original_keywords, $possible_keywords1))
$original_keywords = array("cat", "dog", "mouse");
$possible_keywords1 = preg_split('/,\s*/', "cat, dog, hamster");
echo count(array_intersect($original_keywords, $possible_keywords1))
恏ㄋ傷疤忘ㄋ疼 2024-11-26 09:30:00
$original_keywords = "cat, dog, mouse";
$possible_keywords_1 = "cat, dog, hamster";
$possible_keywords_2 = "cat, sheep, goat";

// Put the input keywords into an array
$keywords = explode(', ', $original_keywords);

// Put all incoming keywords into one big array (you can add as many arrays
// as you want here)
$input = array_merge(
    explode(', ', $possible_keywords_1),
    explode(', ', $possible_keywords_2));

// Count how many times each keywords from $input appears
$count = array_count_values($input);

// Filter out from $count any keyword that is not present in $keywords
$result = array_intersect_key($count, array_flip($keywords));

查看实际操作

$original_keywords = "cat, dog, mouse";
$possible_keywords_1 = "cat, dog, hamster";
$possible_keywords_2 = "cat, sheep, goat";

// Put the input keywords into an array
$keywords = explode(', ', $original_keywords);

// Put all incoming keywords into one big array (you can add as many arrays
// as you want here)
$input = array_merge(
    explode(', ', $possible_keywords_1),
    explode(', ', $possible_keywords_2));

// Count how many times each keywords from $input appears
$count = array_count_values($input);

// Filter out from $count any keyword that is not present in $keywords
$result = array_intersect_key($count, array_flip($keywords));

See it in action.

许仙没带伞 2024-11-26 09:30:00

您可以结合使用 explodearray_intersect

以下是开始的代码:

$possible_keywords_1_array = explode(",", $possible_keywords_1); 
$possible_keywords_2_array = explode(",", $possible_keywords_2); 
$matches = count(array_intersect($possible_keywords_1_array, $possible_keywords_2_array)); 

You can do that using a combination of explode and array_intersect

Following is code to start with:

$possible_keywords_1_array = explode(",", $possible_keywords_1); 
$possible_keywords_2_array = explode(",", $possible_keywords_2); 
$matches = count(array_intersect($possible_keywords_1_array, $possible_keywords_2_array)); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文