2011-05-18 120 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();