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

发布于 2024-11-08 07:31:05 字数 2193 浏览 2 评论 0原文

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

最好的问候,史蒂文

i'm scripting some code that you allows you to upload a txt-file an read this in an array. now i want to change some line of this array with preg_replace but the first array will not change.

here is the code 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>';

?>

Sample Data:

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

best regards, steven

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑寡妇 2024-11-15 07:31:05

您的问题在这里:

$array_liste = array(0);

将其更改为:

$array_liste = array();

Your problem is here:

$array_liste = array(0);

Change it to:

$array_liste = array();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文