【问题标题】:PHP strings - if part of needle is found in haystackPHP 字符串 - 如果在 haystack 中找到针的一部分
【发布时间】:2012-12-05 23:25:00
【问题描述】:

我尝试在网上搜索解决方案,但没有运气(我是 PHP 初学者)。我有两个strings(带有时间戳的电话号码集合)有comma separated values。我需要检查 sting A 逗号之间的特定文本部分是否可以在 string B 中找到。

这是一个刺痛 A 的例子:

9858264-2012-12-05T00:11:28.806Z,1265482-2012-12-05T22:19:49.769Z,9598643-2012-12-05T22:46:17.115Z,

这是一个 og sting B 的示例:

5555649-2012-12-05T22:37:23.765Z,3594595-2012-12-05T22:44:36.363Z,8549851-2012-12-05T22:46:01.259Z,9598643-2012-12-05T22:46:09.600Z


在上述字符串中,以下两个值非常相似(时间戳中只有几秒不同):

From string A: 9598643-2012-12-05T22:46:17.115Z
From string B: 9598643-2012-12-05T22:46:09.600Z


我需要做的是COUNTstring A 中与string B 匹配的逗号之间的值的数量除了时间戳中的最后 5 个字符,因为它们可能是相同的出现但相隔 +/- 几秒钟。


我曾想过使用 php explode 将每个值放入一个数组中,然后进行比较,但是在涉及到数组以及如何将一个数组值与另一个减去最后一个值进行比较时,我现在相当迷茫5 个字符的值。

【问题讨论】:

  • substr($s, 0, -5) 得到字符串减去 5 个字符。
  • 这些列表是否已排序?如果是这样,你的挑战会更轻松。
  • stringA 和 stringB 是否总是在逗号之间包含相同数量的值?
  • @Jack - 谢谢。这将有助于进一步发展。 @psyklopz - 目前它们是随机的。在呼叫中心,这些是错过(刺痛 A)与收到(刺痛 B)的通话记录。 @kennypu - 遗憾的是没有。字符串 A 可以包含任意数量的值,但总是比字符串 B 少(因为已接电话总是大于未接电话)。

标签: php string compare explode


【解决方案1】:

你可以这样做。

function make_string_into_nice_array ($string) {
    $string_array = explode(',',$string);
    return array_map(function($str) {
        return substr($str, 0, -5);    
    }, $string_array);
}

$array_a = make_string_into_nice_array($string_a);
$array_b = make_string_into_nice_array($string_b);

$matched_array = array_intersect($array_a, $array_b);

echo count($matched_array);

【讨论】:

    【解决方案2】:

    试试这个:

    $arrayA = explode(',', $StringA); // Parse the string into array elements, using comma as delimiter.
    $arrayB = explode(',', $StringB);
    
    $matches = array(); // Declare the array that will be the output.
    foreach ($arrayA as $aValue) { // Iterate through each element of A.
        foreach ($arrayB as $bValue) { // Iterate through each element of B.
            // Take the value of element A (chopping off the last 5 characters) and compare with the value of element B (chopping off the last 5 characters)
            if (substr($aValue, 0, strlen($aValue)-5) == substr($bValue, 0, strlen($bValue)-5))) {
                // If there's a match, iterate the count of that particular key in your output array.
                $matches[substr($aValue, 0, strlen($aValue)-5)] += 1;
            }
        }
    }
    echo count($matches); // Print the number of keys that are duped.
    print_r($matches); // Look at each individual key and the number of duplicated instances.
    

    【讨论】:

    • 谢谢。我会检查这个解决方案。
    【解决方案3】:

    老实说,我认为将这两个字符串组合起来,解析它们,然后查找不止一个电话的电话号码会更有用。

    <?php
    $input =
       trim(
          '9858264-2012-12-05T00:11:28.806Z,1265482-2012-12-05T22:19:49.769Z,9598643-2012-12-05T22:46:17.115Z,' .
          '5555649-2012-12-05T22:37:23.765Z,3594595-2012-12-05T22:44:36.363Z,8549851-2012-12-05T22:46:01.259Z,9598643-2012-12-05T22:46:09.600Z',
          ','
       ); //gets rid of leading/trailing commas
    $raw_arr = explode(',', $input);
    
    $phone_records = array();
    
    foreach( $raw_arr as $entry ) {
       $temp = explode('T', $entry);
       $time = $temp[1];
       $temp = explode('-', $temp[0]);
       $phone = array_shift($temp);
       $date = implode('-', $temp);
    
       if( ! isset($phone_records[$phone]) ) {
          $phone_records[$phone] = array();
       }
       $phone_records[$phone][] = array($date, $time);
    }
    
    //print_r($phone_records);
    
    foreach( $phone_records as $number => $entries ) {
       if( count($entries) > 1 ) {
          printf('%s: %s', $number, var_export($entries, TRUE));
       }
    }
    

    输出:

    9598643: array (
      0 =>
      array (
        0 => '2012-12-05',
        1 => '22:46:17.115Z',
      ),
      1 =>
      array (
        0 => '2012-12-05',
        1 => '22:46:09.600Z',
      ),
    )
    

    您还可以使用此代码将每个输入解析为自己的数组,然后使用 foreach 循环运行其中一个并检查值是否为 array_key_exists($arr1_key, $arr2)

    【讨论】:

    • 谢谢。这段代码的一部分实际上帮助我完成了我正在从事的另一个项目:)
    猜你喜欢
    • 2017-12-20
    • 1970-01-01
    • 2020-02-12
    • 2015-10-24
    • 2011-08-10
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多