Php 将带有数字和字母的排序字符串打印到文件中,以查找预匹配解决方案

Php 将带有数字和字母的排序字符串打印到文件中,以查找预匹配解决方案,php,Php,好的,我有一个文本文件,里面有学生编号和相应的名字。我想得到所有这些数据,正确地格式化它们(大写,适当数量的空格,等等),然后把它们放在另一个文件中。原始文本格式有点像这样: 20111101613 XXXXXXXX , XXXX 20111121235 xxXXXX, xxxxxx 20111134234 XXXX, XxxX 20111104142 XXXXXxxxX, XXXX 20111131231 XX , XXXXXX 20111112346 Zoomba, Samthing

好的,我有一个文本文件,里面有学生编号和相应的名字。我想得到所有这些数据,正确地格式化它们(大写,适当数量的空格,等等),然后把它们放在另一个文件中。原始文本格式有点像这样:

20111101613 XXXXXXXX  , XXXX
20111121235 xxXXXX, xxxxxx
20111134234   XXXX, XxxX
20111104142 XXXXXxxxX, XXXX
20111131231 XX , XXXXXX
20111112346 Zoomba, Samthing
20111122953 Acosta, Arbyn
20110111241 Smith, John
20111412445 Over, Flow Stack
20111112345 foo, BAR
20111122953 ACOSTA, ARBYN
20111112345 FOO, BAR
20111412445 OVER, FLOW STACK
20110111241 SMITH, JOHN
20111112346 ZOOMBA, SAMTHING
例如: 输入文件内容如下所示:

20111101613 XXXXXXXX  , XXXX
20111121235 xxXXXX, xxxxxx
20111134234   XXXX, XxxX
20111104142 XXXXXxxxX, XXXX
20111131231 XX , XXXXXX
20111112346 Zoomba, Samthing
20111122953 Acosta, Arbyn
20110111241 Smith, John
20111412445 Over, Flow Stack
20111112345 foo, BAR
20111122953 ACOSTA, ARBYN
20111112345 FOO, BAR
20111412445 OVER, FLOW STACK
20110111241 SMITH, JOHN
20111112346 ZOOMBA, SAMTHING
输出文件内容应如下所示:

20111101613 XXXXXXXX  , XXXX
20111121235 xxXXXX, xxxxxx
20111134234   XXXX, XxxX
20111104142 XXXXXxxxX, XXXX
20111131231 XX , XXXXXX
20111112346 Zoomba, Samthing
20111122953 Acosta, Arbyn
20110111241 Smith, John
20111412445 Over, Flow Stack
20111112345 foo, BAR
20111122953 ACOSTA, ARBYN
20111112345 FOO, BAR
20111412445 OVER, FLOW STACK
20110111241 SMITH, JOHN
20111112346 ZOOMBA, SAMTHING
编辑:有人能给我一个关于如何使用正则表达式生成此函数的提示或解决方案吗

function sortslist($infile, $outfile)
{
    // open the input file named conversion.txt
    $inptr = fopen($infile, "r");
    if (!$inptr)
    {
        trigger_error("File cannot be opened: $infile", E_USER_ERROR);
    }

    // initialize student number to zero
    $snum = 0;

    // number of letters in the name string
    $n = 0;

    // initialize the name string to be empty
    $name = "";

    // iteratively scan the input file
    $done = false;
    while (!$done)
    {
        // get each character in the file
        $c = fgetc($inptr);

        // if the character is a digit, add it to the student number
        if (ctype_digit($c))
        {
            $snum = (($snum * 10) + ($c - '0'));
        }

        // else, add to name string including commas and space. Input file might have tabs
        else if (ctype_alpha($c) || ($n > 0 && ($c == " " || $c == "\t")) || $c == ",")
        {
            // append the new character to name string
            $name .= $c;

            // add a space after the comma
            if ($c == ",")
            {
                $name .= " ";
                $n++;
            }

            // increment the number of letters
            $n++;
        }

        // end of the line
        else if ($c == "\n" || !$c)
        {
            // 0 is added to student numbers when newline is detected so neglect them
            if ($snum != 0 && $name != "\n")
            {
                // replace consecutive spaces with one space only
                $name = preg_replace(['/\s\s+/', '/\s,/', '/(\s*)(?>$)/'], [' ', ',', ''], $name);

                // record student number and name
                $info['snum'][] = $snum;
                $info['name'][] = strtoupper($name);
            }

            // reset the values needed
            $snum = 0;
            $n = 0;
            $name = "";

            // if we hit the end of the file then it is done
            if (!$c)
            {
                $done = true;
            }
        }
    }

    // sort the students names alphabetically
    array_multisort($info['name'], $info['snum']);

    // combine the name strings and there corresponding student number
    $i = 0;
    $students = [];
    $last_student = end($info['snum']);
    foreach ($info['snum'] as $snum)
    {
        $students[$snum] = $snum . " " . $info['name'][$i++];

        // update input file too
        fwrite($inptr, $students[$snum]);

        // don't add a newline to the end of the file
        if ($snum != $last_student)
        {
            $students[$snum] .= "\n";
        }
    }

    // put it into a new file called slist.txt
    file_put_contents($outfile, $students, LOCK_EX);

    // close the input file
    fclose($inptr);
}

您的问题在于,
$hashtable
值与字符串中的学生ID一起存储
asort()
将始终查看值的开头,并根据该开头进行排序。因此,为了按姓名排序,您必须将学生ID和姓名分成两个单独的数组,然后使用
array\u multisort()
对它们进行排序

替换:

$hashtable[$snum] = $snum . " " . strtoupper($name) . "\n";
与:


更恰当地解释脚本的输入和输出应该是什么会有帮助。Hmmm。。我没有想到把它们结合起来:P我尝试你的方法时遇到的问题是如何轻松地将两个数组的值组合到文件中。但在我照你说的做了之后,它称为很多偏移错误:/已经解决了!:)由于某种原因,你的解决方案不起作用。。因为装载时间太长了。。。但我确实做了一些调整:D谢谢你的想法!我会把答案贴出来的。我没有测试代码,只是从我的脑子里选出来的。。很高兴能帮你解决:)