1

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

0100014746510106200140001020061780000000041666670072860103508101 0100008030950106200270002020139450000000020000006663540105338500 0100004586400106200270002020206660000000003700000511890102603900 0100008204530106200270002020218320000000011666670014450101008906 0100015309660106200270002021023010000000019400001666460105319807

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

010001
010000
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";
 }

}
4

5 回答 5

5

介绍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);

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

于 2013-09-06T03:58:49.493 回答
3

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

只是加入一个单行(将适用于 php 5.3 及更高版本)。这假设您的输入是 $str 并且行将包含每行前 6 个字符的换行符分隔字符串。

$lines = implode("\n", array_map(function($a){return substr($a, 0, 6); }, explode("\n",$str)));
于 2013-09-06T03:59:42.180 回答
2

代码:

$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
于 2013-09-06T03:58:45.337 回答
1

基本上:

$x=explode("\n",$result); //new line will depend on os ("\r\n"|"\r")

foreach($x as $line){
//substr $line
}
于 2013-09-06T03:54:13.157 回答
1

尝试:

<?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\n'。

于 2013-09-06T03:54:49.837 回答