Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
如何在php中从数组回送?_Php_Arrays_Count_Echo_Strlen - Fatal编程技术网

如何在php中从数组回送?

如何在php中从数组回送?,php,arrays,count,echo,strlen,Php,Arrays,Count,Echo,Strlen,我有一个文本文件,其中的数字如下: 12345678901234567890 123456789012345678901 123456789012345678902 1234567890123456789012 1234567890123456789023 12345678901234567890123 12345678901234567890234 我写了一个脚本逐行读取这个文件。我想计算行中的字符,而不仅仅是字节,并且只选择包含21或22个字符的行 使用下面的脚本可以工作。别问我为什么我说

我有一个文本文件,其中的数字如下:

12345678901234567890
123456789012345678901
123456789012345678902
1234567890123456789012
1234567890123456789023
12345678901234567890123
12345678901234567890234
我写了一个脚本逐行读取这个文件。我想计算行中的字符,而不仅仅是字节,并且只选择包含21或22个字符的行

使用下面的脚本可以工作。别问我为什么我说的是23个字,它却读了21个字。我认为这与文件编码有关,因为strlen只给了我字节

在选择长度为21或22个字符的行之后,我需要拆分该行。如果是21,它应该变成两个字符串(一个15字符的字符串和一个6字符的字符串), 如果是22个字符,则应将其拆分为16个字符的字符串和6个字符的字符串

我尝试在数组中创建它,但数组显示如下内容:

Array ( [0] => 123456789012345 [1] => 678901 ) Array ( [0] => 123456789012345 [1] => 678903 )
我想让它显示如下:

123456789012345=678901
123456789012345=678903
知道我如何从阵列中回音吗

$filename = "file.txt";
$fp = fopen($filename, "r") or die("Couldn't open $filename");

while (!feof($fp)){
    $line = fgets($fp);
    $str = strlen($line);
    if($str == 23){
        $str1=str_split($line, 15);
        print_r($str1);
        foreach ($str1 as $value)
        {
           echo $value . "=" ;
        }
    }
    if($str == 24){
        $str1=str_split($line, 16);

        foreach ($str1 as $value)
        {
            echo $value . "=" ;
        }
    }

}
只是一些提示:

$filename = "file.txt";
$lines    = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines === FALSE) {
   die "Couldn't open $filename";
}

foreach ($lines as $line)
{
    $length = strlen($line);

    if ($length < 21 || $length > 22)  {
        continue;
    }

    $start = 15;
    if ($length === 22) {
        $start = 16;
    }

    echo substr($line, 0, $start), '=', substr($line, $start), "\n";
}

本例直接使用并执行
$start
计算。

作为最基本的方式:
echo$arr[0].='$arr[1]
…?您可能还希望阅读关于读取和无数组拆分的内容。
$filename = "file.txt";
$lines    = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines === FALSE) {
   die "Couldn't open $filename";
}

foreach ($lines as $line)
{
    $start = strlen($line) - 6;

    if ($start === 15 || $start === 16)
    {
        echo substr_replace($line, '=', $start, 0), "\n";
    }

}