douling8772 2017-12-17 23:51
浏览 74
已采纳

PHP如何比较两个变量之间的字符串并只显示唯一的字符串?

NOTE: here in the S.O. there are many ways to do this with array keys, between values/words of the same variables, between two different arrays, but I see nothing on the internet between strings and not arrays of two different variables.

<?php
$a = 'this car is very beautiful and is the fast';
$b = 'this red car is very beautiful and is the fast that others';
var_export($unique_words = show_unique_strings($a, $b));
//expected output(painted on the screen): red that others
?>
  • 写回答

2条回答 默认 最新

  • duanbing8817 2017-12-18 00:04
    关注

    As Siphalor stated here is an implementation

    $a = 'this car is very beautiful and is the fast';
    $b = 'this red car is very beautiful and is the fast that others';
    echo $unique_words = show_unique_strings($a, $b);
    //expected output(painted on the screen): red that others
    
    function show_unique_strings($a, $b) {
      $aArray = explode(" ",$a);
      $bArray = explode(" ",$b);
      $intersect = array_intersect($aArray, $bArray);
      return implode(" ", array_merge(array_diff($aArray, $intersect), array_diff($bArray, $intersect)));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?