简体   繁体   English

如何从php中的数组回显?

[英]How to echo from an array in php?

I have a text file, with digits in it like the following:我有一个文本文件,其中的数字如下所示:

12345678901234567890
123456789012345678901
123456789012345678902
1234567890123456789012
1234567890123456789023
12345678901234567890123
12345678901234567890234

I wrote a script to read this file line by line.我写了一个脚本来逐行读取这个文件。 I want to count the characters in the line, not just the bytes, and and only select the lines that contain 21 or 22 characters.我想计算行中的字符,而不仅仅是字节,并且只选择包含 21 或 22 个字符的行。

Using the script below works.使用下面的脚本有效。 Don't ask me why it read 21 characters when I said 23. I think it's something to do with file encoding since strlen only got me the bytes.当我说 23 时,不要问我为什么它读取 21 个字符。我认为这与文件编码有关,因为strlen只为我提供了字节。

After selecting the lines that are of length 21 or 22 characters, I need to split the line.选择长度为 21 或 22 个字符的行后,我需要拆分该行。 If it's 21, it should become two strings (a 15 character string and a 6 character string), and if it is 22 characters should be split into a 16 character string and a 6 character string.如果是21,就应该变成两个字符串(一个15个字符的字符串和一个6个字符的字符串),如果是22个字符,应该分成一个16个字符的字符串和一个6个字符的字符串。

I tried making it in an array, but the array shows something like this:我试着把它放在一个数组中,但数组显示如下:

Array ( [0] => 123456789012345 [1] => 678901 ) Array ( [0] => 123456789012345 [1] => 678903 )

I want it to show like this instead:我希望它显示如下:

123456789012345=678901
123456789012345=678903

Any idea how I can echo from an array?知道如何从数组中回声吗?

$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 . "=" ;
        }
    }

}

Just some pointers:只是一些提示:

$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";
}

That is using file and substr .那是使用filesubstr In PHP a character is one byte.在 PHP 中,一个字符是一个字节。


A different example:一个不同的例子:

$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";
    }

}

This example is using substr_replace and does the $start calculation directly upfront.此示例使用substr_replace并直接预先进行$start计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM