douxuexiao1748 2019-05-13 13:27
浏览 190
已采纳

字符串比较,无论PHP顺序如何

I want to know if there is a way to compare by means of some PHP method, and verify that two chains regardless of the order are the same,

for example:

     $string1 = "Pedro Perez";
     $string2 = "Perez Pedro";

So my question is how can I make this TRUE in a comparison. Clearly, your order makes it different.


I ask this question because I am developing a search, and I want a comparison of $string1 and $string2 to be taken as the same value, regardless of the order they have, as long as the string has the same words

  • 写回答

5条回答 默认 最新

  • dqj29136 2019-05-13 13:31
    关注

    You can try doing this.

    1. Convert the string into an array by splitting on spaces.
    2. Sort the array.
    3. Convert back the array into string.
    4. Compare both.

    In simple ways, here you go...

    <?php
      $string1 = "Pedro Perez";
      $string2 = "Perez Pedro";
      $string1 = explode(" ", $string1);
      $string2 = explode(" ", $string2);
      // Update: You don't save the output of sort, as it's by reference.
      sort($string1);
      sort($string2);
      $string1 = implode(" ", $string1);
      $string2 = implode(" ", $string2);
      var_dump($string1 === $string2);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型