2013-02-03 113 views
0

我在php中讀取一個txt文件。問題是我可以除了它的空間。我使用了explode()函數,但它不起作用。我嘗試了所有的方法來閱讀,但沒有對。php讀取空格的txt文件

$a = file_get_contents($file_url, FILE_USE_INCLUDE_PATH); 
$a = explode(" ",$a);//When I call this, all of value are added to $a[0] as an array 
$a = explode("\n",$a[0]); // I called this to explode "enter" but it doesn't work too 

現在,在調用上面的代碼後,$ a [0]是「85」,$ a [1]是「94 16」。當我打電話給$ a [1] [0]時,它是「9」而不是「94」。

在TXT文件中的數據是一個三角形在1994年IOI問題:

85 
94 16 
15 63 72 
55 78 89 105 
77 24 56 93 17 
... 

這是我想用http://www.mediafire.com/?kjdjflvf0665q03

文件,這是我的代碼來解決問題三角形從txt文件此數據

$t = array(); 
$count = 0; 
$l = strlen($a[0]); 
$d = 0;//get the quantity of rows in txt file 
while($l>0){ 
    $d++; 
    $l = $l - $d; 
} 
$f = array(); 
$prevX = $prevY = $val = 0; 
//add all of values to array $t[] as a type: $t[row][col] 
for($i =0 ; $i<$d; $i++){ 
    for($j=0; $j<=$i; $j++){ 
     $t[$i][$j] = $a[0][$count++]; 
    } 
} 
//start the beginning points: $f[row+1][col] = ($t[row+1][col]+MAX(f[row][col],f[row][col-1]),prevX, prevY, $t[row+1][col]) 
$f[0][0] = array($t[0][0],$prevX,$prevY, $t[0][0]); 
$f[1][0] = array(($t[1][0]+$f[0][0][0]),0,0,$t[1][0]); 
$f[1][1] = array(($t[1][1]+$f[0][0][0]),0,0,$t[1][1]); 
$i=2; 
while($i<$d){ 
    for ($j=0; $j <= $i; $j++){ 
     if(($j-1)<0){//check to except the value which not undefined, but may be not works exactly 
      $f[$i][$j] = array(($f[$i-1][$j][0]+$t[$i][$j]),$i-1,$j,$t[$i][$j]); 
     } 
     else { 
      if($j>($i-1)){//check to except the value which not undefined, but may be not works exactly 
       $f[$i][$j] = array(($f[$i-1][$j-1][0]+$t[$i][$j]),$i-1,$j-1,$t[$i][$j]); 
      } 
      else{ 
       if($f[$i-1][$j][0]<$f[$i-1][$j-1][0]){ 
        $f[$i][$j] = array(($f[$i-1][$j-1][0]+$t[$i][$j]),$i-1,$j-1,$t[$i][$j]); 
       } 
       else if($f[$i-1][$j][0]>$f[$i-1][$j-1][0]){ 
        $f[$i][$j] = array(($f[$i-1][$j][0]+$t[$i][$j]),$i-1,$j,$t[$i][$j]); 
       } 
      } 
     } 
    } 
    $i++; 
} 
//print_r($f); 
$result=0; 
$x = $y = $finalR = 0; 
for($j=0;$j<$d;$j++){ 
    if($f[$d-1][$j][0]>$result){ 
     $result = $f[$d-1][$j][0]; 
     $x = $d-1; 
     $y = $j; 
     $finalR = $f[$d-1][$j][3]; 
    } 
} 
while($x>0){ 
    echo '(',$x,',',$y,')',$f[$x][$y][3],'<----'; 
    $x = $f[$x][$y][1]; 
    $y = $f[$x][$y][2]; 
} 
echo '(0,0)', $f[0][0][3]; 
+2

請包括我加入它,JohnP文件 – JohnP

+0

的內容。請幫幫我。謝謝 – JoeNguyen

+0

請,我需要幫助 – JoeNguyen

回答

0

這是不可能有$一個[1]爲 「94 16」,並有$一個[1] [0]作爲94因爲$ A [1]是串和$ a [1]中的$ a [1] [0]是一個數組鍵。

這是很容易使兩個陣列,一個用於行和一個用於參考表

<? 
$file = file('file.txt'); 
foreach($file as $i => $line) 
{ 
    foreach(explode("\t",$line) as $num) 
    { 
     $num = trim($num); 
     if($num!='') 
     { 
      $a[$i][] = $num; 
     } 
    } 
} 
echo '<pre>'; 
pre($a); 

//full rows examples 
pre(get_ref('0',$a)); 
pre(get_ref('1',$a)); 
pre(get_ref('2',$a)); 

//individual cells examples 
pre(get_ref('0,0',$a)); 
pre(get_ref('1,0',$a)); 
pre(get_ref('1,1',$a)); 
pre(get_ref('2,0',$a)); 
pre(get_ref('2,1',$a)); 
pre(get_ref('3,2',$a)); 

function get_ref($ref,$a) 
{ 
    $ref = explode(',',$ref); 
    foreach($ref as $r) 
    { 
     $a = $a[$r]; 
    } 
    if(is_array($a)) 
    { 
     return(implode("\t",$a)); 
    }else{ 
     return($a); 
    } 

} 


function pre($d) 
{ 
    echo '<pre>'; 
    print_r($d); 
    echo '</pre>'; 
} 
?> 
+0

謝謝,我明白了。但現在當我運行它時,我得到了這個: 注意:未定義的偏移量:...在C:\ ... \ myfile.php在線...我應該怎麼做? – JoeNguyen

+0

使用上面的新代碼或讓我知道哪一行:) –

+0

導致我用來添加$ f值的代碼。在開始點後的While循環中 – JoeNguyen