2011-05-18 37 views
0

我正在腳本化一些代碼,您允許您上傳一個txt文件並將其讀入數組中。現在我想用preg_replace改變這個數組的某一行,但第一個數組不會改變。使用preg_replace替換某些數組(第一行不起作用)

這裏是代碼http://pastebin.com/jkxAE1h5

HTML:

<form method="post" enctype="multipart/form-data"> 
<input name="my_text[]" type="file" /> 
<input type="submit" /> 

PHP:

<?php 
$geladene_seite = $_FILES['my_text']['tmp_name'][0]; 
$array_liste = array(0); 
$my_text_file = file($geladene_seite); 

foreach ($my_text_file as $single_file) { 
    $single_file = trim($single_file); // leerzeichen entfernen 
    if (strlen($single_file) > 0){ 
    $single_file = str_replace(chr(10), '', $single_file); 
    $single_file = str_replace(chr(13), '', $single_file); 
    $menge = array_push ($array_liste, $single_file); 
    } 
} 

echo '<hr><pre>'; 
print_r ($array_liste); 
echo '</pre>'; 

$text1 = str_replace($array_liste[1], '<b>'.$array_liste[1].'</b>', $array_liste[10]); 
$text2 = str_replace($array_liste[2], '<b>'.$array_liste[2].'</b>', $array_liste[11]); 
$text3 = str_replace($array_liste[3], '<b>'.$array_liste[3].'</b>', $array_liste[12]); 

echo '<p>'.$text1.'</p>'; 
echo '<p>'.$text2.'</p>'; 
echo '<p>'.$text3.'</p>'; 

?> 

樣本數據:

text one 
text two 
text three 

this is for description one 
this is for description two 
this is for description three 

header for number one 
header for number two 
header for number three 

many more infos for the text one many more infos for the text one many more infos for the text one many more infos for the text one many more infos for the text one many more infos for the text one 
many more infos for the text two many more infos for the text two many more infos for the text two many more infos for the text two many more infos for the text two many more infos for the text two 
many more infos for the text three many more infos for the text three many more infos for the text three many more infos for the text three many more infos for the text three many more infos for the text three 

最好的問候,史蒂芬

+4

有一個在你的代碼中沒有的preg_replace。 – AndersTornkvist 2011-05-18 19:59:28

+1

您能否簡單地提出這個問題?輸入的細節,你期望輸出是什麼以及它的真實性。 – 2011-05-18 20:00:38

+0

不重要,如果str_replace或preg_replace。這是我的代碼中的相同錯誤。這裏是一個圖像http://img834.imageshack.us/img834/8179/arrayu.jpg 我在數組1中的東西是一個小點。誰能說這是什麼? thx爲您提供幫助 – 2011-05-18 20:08:05

回答

0

你的問題是在這裏:

$array_liste = array(0); 

將其更改爲:

$array_liste = array();