用preg_replace替换一些数组(第一行不起作用)

时间:2011-05-18 19:55:12

标签: php arrays preg-replace

我正在编写一些代码,允许您上传一个txt文件,并在数组中读取它。现在我想用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
最好的问候,史蒂文

1 个答案:

答案 0 :(得分:0)

你的问题在这里:

$array_liste = array(0);

将其更改为:

$array_liste = array();