PHP 比较字符串并返回公共值

发布于 2024-11-25 09:52:02 字数 250 浏览 0 评论 0原文

我试图找到与此相关的其他帖子/信息,但它们似乎都不起作用 - 尽管我确信这是一项简单的任务。

我有两个字符串,我想要一些代码行来给出它们的共同点。

例如,我可能有...

String1 = "Product Name - Blue";
String2 = "Blue Green Pink Black Orange";

并且我想要一个仅包含值 Blue 的字符串。我该怎么做?提前致谢!

I have tried to find other posts/information on this and none of them seem to work - although I'm sure this is a simple task.

I have two strings, and I would like to have some lines of code that give me the word that they have in common.

For example, I may have...

String1 = "Product Name - Blue";
String2 = "Blue Green Pink Black Orange";

And I would like to have a string only containing the value Blue. How can I do this? Thanks in advance!

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

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

发布评论

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

评论(3

乖乖公主 2024-12-02 09:52:03

您可以使用 explodearray_intersect 也许?

此处演示 & 此处

<?php
 
  function common($str1,$str2,$case_sensitive = false)
  {
    $ary1 = explode(' ',$str1);
    $ary2 = explode(' ',$str2);

    if ($case_sensitive)
    {
      $ary1 = array_map('strtolower',$ary1);
      $ary2 = array_map('strtolower',$ary2);
    }

    return implode(' ',array_intersect($ary1,$ary2));
  }
 
  echo common('Product Name - Blue','Blue Green Pink Black Orange');

返回“Blue”;

编辑如果您愿意,请更新它以包含不区分大小写的版本。

You can use explode and array_intersect maybe?

Demo here & here

<?php
 
  function common($str1,$str2,$case_sensitive = false)
  {
    $ary1 = explode(' ',$str1);
    $ary2 = explode(' ',$str2);

    if ($case_sensitive)
    {
      $ary1 = array_map('strtolower',$ary1);
      $ary2 = array_map('strtolower',$ary2);
    }

    return implode(' ',array_intersect($ary1,$ary2));
  }
 
  echo common('Product Name - Blue','Blue Green Pink Black Orange');

Returns "Blue";

EDIT Updated it to include a case-insensitive version if you'd like it.

靖瑶 2024-12-02 09:52:03

解决方案是将字符串拆分为两个单词数组 - 使用 explode()< /code>,例如:

$string1 = "Product Name - Blue";
$string2 = "Blue Green Pink Black Orange";

$arr1 = explode(' ', $string1);
$arr2 = explode(' ', $string2);

请注意,explode() 是一个基本解决方案;您可能想使用更复杂的东西,例如 preg_split() ,它允许使用更具体的分隔符。

And, then, to use [**`array_intersect()`**][3] on those arrays, to find out which words are present in both :

$common = array_intersect($arr1, $arr2);
var_dump($common);

Which, in this case, would give :

array
  3 => string 'Blue' (length=4)

A solution would be to split your strings into two arrays of words -- using explode(), for instance :

$string1 = "Product Name - Blue";
$string2 = "Blue Green Pink Black Orange";

$arr1 = explode(' ', $string1);
$arr2 = explode(' ', $string2);

Note that explode() is a basic solution ; you might want to use something a bit more complex, like preg_split(), which allows for more specific delimiters.

And, then, to use [**`array_intersect()`**][3] on those arrays, to find out which words are present in both :

$common = array_intersect($arr1, $arr2);
var_dump($common);

Which, in this case, would give :

array
  3 => string 'Blue' (length=4)
耳根太软 2024-12-02 09:52:03

您想要对每个列表执行explode() 将它们分成数组,然后使用array_intersect() 查找两个数组中的常见单词。

You want to do an explode() on each list to separate them into arrays, then use array_intersect() to find the common words in both arrays.

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