【问题标题】:replace some arrays with preg_replace (first line doesn't work)用 preg_replace 替换一些数组(第一行不起作用)
【发布时间】:2011-05-18 19:55:12
【问题描述】:

我正在编写一些代码,您可以通过这些代码上传一个 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

最好的问候,史蒂文

【问题讨论】:

  • 您的代码中没有 preg_replace。
  • 你能把问题简单一点吗?输入的详细信息、您期望的输出以及实际的输出。
  • 不管是 str_replace 还是 preg_replace。这是我的代码中的相同错误。这是一个图像img834.imageshack.us/img834/8179/arrayu.jpg 数组 1 中的我的东西是一个小点。谁能说这是什么?谢谢你的帮助

标签: php arrays preg-replace


【解决方案1】:

你的问题在这里:

$array_liste = array(0);

改成:

$array_liste = array();

【讨论】:

    猜你喜欢
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 2011-10-07
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多