Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据str len-php对file.txt的行进行排序_Php - Fatal编程技术网

如何根据str len-php对file.txt的行进行排序

如何根据str len-php对file.txt的行进行排序,php,Php,我想根据字符串len对file.txt进行排序 示例文件.txt: wew 12dde 12swsw 11 swsw a ofter排序文件sorted.txt: a 11 wew swsw 12dde 12swsw 这应该满足您的需要: <?php function lensort($a, $b) { return strlen($a) - strlen($b); } $path1 = '/root/oldfile.txt'; $path2 = '/root/newfile

我想根据字符串len对file.txt进行排序

示例文件.txt:

wew
12dde
12swsw
11
swsw
a
ofter排序文件sorted.txt:

a
11
wew
swsw
12dde
12swsw

这应该满足您的需要:

<?php
function lensort($a, $b) {
    return strlen($a) - strlen($b);
}

$path1 = '/root/oldfile.txt';
$path2 = '/root/newfile.txt';
file_put_contents($path2, usort(file($path1), 'lensort'));

这应该满足您的需要:

<?php
function lensort($a, $b) {
    return strlen($a) - strlen($b);
}

$path1 = '/root/oldfile.txt';
$path2 = '/root/newfile.txt';
file_put_contents($path2, usort(file($path1), 'lensort'));

tanX,如何在第页回显此参数?tanX,如何在第页回显此参数?
<?php
function lensort($a, $b) {
    return strlen($a) - strlen($b);
}

$path1 = '/root/oldfile.txt';
$path2 = '/root/newfile.txt';
file_put_contents($path2, usort(file($path1), 'lensort'));