子字符串的每一行


Substring each line of the string

在我的$result变量中,我有一个 5 行字符串:

01000147465101062001400010200617800000000416666700728601035081010100008030950106200270002020139450000000020000006663540105338500010000458640010620027000202020666000000000370000051189010260390001000082045301062002700020202183200000000116666700144501010089060100015309660106200270002021023010000000019400001666460105319807

我怎样才能substr()每一行...为了得到这个结果:


010001010000
010000
010000

010001

只需要获取每行的前 6 列...请帮忙...

PHP代码:

$file = 'upload/filter.txt';
$searchfor = $_POST['search'];
$filename = $_POST['filename'];
$btn = $_POST['button'];

if($btn == 'GENERATE' ) {
//prevents the browser from parsing this as HTML.
//header('Content-Type: text/plain');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=".$filename.".txt ");
header("Content-Transfer-Encoding: binary ");

// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
 $pattern = "/^.*$pattern.*'$/m";

 // search, and store all matching occurences in $matches
 if(preg_match_all($pattern, $contents, $matches)){
 $result = implode("'n", $matches[0]);
 echo $result;

  }
else{
 echo "No matches found";
 }
}

介绍strtok()

$text = <<<EOM
0100014746510106200140001020061780000000041666670072860103508101
0100008030950106200270002020139450000000020000006663540105338500
0100004586400106200270002020206660000000003700000511890102603900
0100008204530106200270002020218320000000011666670014450101008906
0100015309660106200270002021023010000000019400001666460105319807
EOM;
for ($line = strtok($text, "'n"); $line !== false; $line = strtok("'n")) {
        echo substr($line, 0, 6), "'n";
}

该函数strtok()基于标记(或分隔符)对字符串进行分块;在此代码中,每一行都是一个块,对于每个块,您只显示前六个字符,后跟换行符。

更新

您也可以像这样使用preg_replace()

echo preg_replace('/^(.{6}).*/m', '$1', $text);

它捕获前六个字符,然后捕获行的其余字符;然后使用内存捕获来执行替换。

注意 - 在所有发布的正确答案中,这可能是内存效率最低的,但嘿,这是一行。

只是为了加入一行代码(将在 php 5.3 及更高版本上运行)。这假设您的输入是$str并且行将包含一个换行符分隔的字符串,该字符串包含每行的前 6 个字符。

$lines = implode("'n", array_map(function($a){return substr($a, 0, 6); }, explode("'n",$str)));

代码:

$str = "0100014746510106200140001020061780000000041666670072860103508101'n"
      ."0100008030950106200270002020139450000000020000006663540105338500'n"
      ."0100004586400106200270002020206660000000003700000511890102603900'n"
      ."0100008204530106200270002020218320000000011666670014450101008906'n"
      ."0100015309660106200270002021023010000000019400001666460105319807'n";
if( preg_match_all('#^'d{6}#m',$str,$matches) ){
  echo join("'n",$matches[0]);
}

结果:

010001
010000
010000
010000
010001

基本上:

$x=explode("'n",$result); //new line will depend on os ("'r'n"|"'r")
foreach($x as $line){
//substr $line
}

尝试:

<?php
$str = explode(PHP_EOL, "
        0100014746510106200140001020061780000000041666670072860103508101 
        0100008030950106200270002020139450000000020000006663540105338500 
        0100004586400106200270002020206660000000003700000511890102603900 
        0100008204530106200270002020218320000000011666670014450101008906 
        0100015309660106200270002021023010000000019400001666460105319807
");
for($i = 0; $i<count($str);$i++){
    if($str[$i] != NULL)
        echo substr((string)$str[$i],0,9);
}
?>

PHP_EOL =>此平台的正确"行尾"符号。自 PHP 4.3.10 和 PHP 5.0.2 起可用

PHP : 预定义常量

如果PHP_EOL不可用,也可以使用"''r'"。