子串字符串的每一行

时间:2013-09-06 03:46:59

标签: php string substring

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

}

5 个答案:

答案 0 :(得分: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);

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

答案 1 :(得分:3)

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

只是投入一个单行(将在php 5.3及以上版本上工作)。假设您的输入为$ str,而行将包含每行前6个字符的换行符分隔字符串。

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

答案 2 :(得分: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

答案 3 :(得分:1)

基本上:

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

foreach($x as $line){
//substr $line
}

答案 4 :(得分: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 =&gt; 此平台的正确“行尾”符号。自PHP 4.3.10和PHP 5.0.2起可用

PHP : Predefined Constants

如果PHP_EOL不可用,您也可以使用'\ r \ n'。