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 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算