PHP,从数组读取时出现 str_replace 问题

发布于 2024-10-01 07:33:56 字数 962 浏览 8 评论 0原文

我是 php 新手,我正在尝试编写一个脚本来读取 CSV 文件(file1.csv),并将文件中的单词与 html 文件(file2.html)中的单词进行比较(如果 file2.html 中的单词匹配)对于 file1.csv 中的关键部分,它应该更改 file2.html 内容与匹配的键的值..

到目前为止我所做的是:

$glossArray = array();
$file_handle = fopen("file1.csv", "r");
while (!feof($file_handle) ) {

    $line_of_text = fgetcsv($file_handle, 10000,';');
    $glossArray[$line_of_text[0]] =  $line_of_text[1];
    $counter++;
}
fclose($file_handle);

$file = file_get_contents("file2.html");

foreach($glossArray as $key => $value){
    $results = str_replace($key," means ".$value ,$file);
}

echo $results;

我认为当我尝试迭代和更改值时会出现我的问题..因为我看到的只是 file2.html 的内容未更改,

任何帮助将不胜感激,

提前

Nader

谢谢 在您提出宝贵建议后,我用新代码编辑了旧代码..现在是这样的..但仍然不起作用。

更新:用 : 更改 foreach

$results = str_replace(array_keys($glossArray), "means ".array_values($glossArray), $file);

解决了问题..但出现了另一个问题:现在每次替换字符串时,都会在其前面添加单词“Array”。

i am new to php, and i am trying to do a script that reads an CSV file(file1.csv) and compare the words in the file with words in a html file (file2.html), if word in file2.html match with the key part in file1.csv it should change the file2.html contents with the value of the key matched ..

what i have done so far is this :

$glossArray = array();
$file_handle = fopen("file1.csv", "r");
while (!feof($file_handle) ) {

    $line_of_text = fgetcsv($file_handle, 10000,';');
    $glossArray[$line_of_text[0]] =  $line_of_text[1];
    $counter++;
}
fclose($file_handle);

$file = file_get_contents("file2.html");

foreach($glossArray as $key => $value){
    $results = str_replace($key," means ".$value ,$file);
}

echo $results;

i think that my problem occurs when i try to iterate and change values .. because what i see is only the contents of file2.html unchanged

any help would be appreciated

thank you in advance

Nader

P.s. i edited the old code with the new one after your valuable advise .. now it's like this .. but still doesnt work.

Update: changing the foreach with :

$results = str_replace(array_keys($glossArray), "means ".array_values($glossArray), $file);

solved the problem .. but another one comes up: now every time it replaces a string it adds the word 'Array' ahead of it.

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

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

发布评论

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

评论(5

十级心震 2024-10-08 07:33:57

新问题的解决方案(这实际上应该是它自己的问题,而不是对现有问题的编辑)是 array_values 返回一个数组,当您将数组与字符串连接时,php 会插入 '数组'而不是值。

$results = str_replace(array_keys($glossArray), "means ".array_values($glossArray), $file);

是不正确的。你应该这样做:

$vals = array_values($glossArray);
foreach($vals as $k=>$v)$vals[$k] = 'means '.$v;
$results = str_replace(array_keys($glossArray), $vals, $file);

注意,glossArray 的值被提取,并且每个值都与你的字符串连接 - 如果你只是尝试将字符串与数组连接,你将得到一个字符串,而不是一个数组。

The solution to your new problem (which should really be it's own question, not an edit of the existing one) is that array_values returns an array, and when you concatenate an array with a string, php inserts 'Array' instead of the value.

$results = str_replace(array_keys($glossArray), "means ".array_values($glossArray), $file);

is incorrect. You should do this instead:

$vals = array_values($glossArray);
foreach($vals as $k=>$v)$vals[$k] = 'means '.$v;
$results = str_replace(array_keys($glossArray), $vals, $file);

Notice that the values of glossArray are extracted, and each value concatenated with your string - if you just try and concatenate the string with the array, you'll get a string, not an aray.

迟月 2024-10-08 07:33:56

您每次都将整个 $glossArray 传递给 str_replace 。每次执行 str_replace 时,您还会传递初始文件内容,因此最多您会看到一个替换。我认为您想更改为这样的内容:

$results = $file;
foreach($glossArray as $index=>$value)
{
    $results = str_replace($index,$value ,$results);

}

由于 str_replace 允许前两个参数使用数组(正如另一位用户提到的),您也可以执行类似的操作而不是循环:

$results = str_replace(array_keys($glossArray), array_values($glossArray), $file);

You're passing the entire $glossArray in to str_replace each time. You're also passing the initial file contents in each time you do str_replace, so at most you'd see one replacement. I think you want to change to something like this:

$results = $file;
foreach($glossArray as $index=>$value)
{
    $results = str_replace($index,$value ,$results);

}

Since str_replace allows arrays for the first two parameters (as another user mentions) you could also do something like this instead of a loop:

$results = str_replace(array_keys($glossArray), array_values($glossArray), $file);
涙—继续流 2024-10-08 07:33:56

是的,问题出在你的第二个 foreach 上。它应该这样读:

foreach($glossArray as $key => $value){
    $results = str_replace($key,$value ,$file);
}

您忘记了密钥,因此它将用 $value 替换 $glossArray 中每个值的每个实例。祝你好运!

Yes, the problem is in your second foreach. It should read like this:

foreach($glossArray as $key => $value){
    $results = str_replace($key,$value ,$file);
}

You forgot the key, so it's replacing every instance of every value in $glossArray with the $value. Good luck with that!

莳間冲淡了誓言ζ 2024-10-08 07:33:56

为什么要打开 file2.html 进行读写,然后抓取其中的内容?

(顺便说一句 - 这在具有严格锁定的系统上会出现严重错误)

foreach($glossArray as $value)
{
  $results = str_replace($glossArray,$value ,$file);

我认为这应该是

foreach($glossArray as $old=>$new)
{
   $results = str_replace($old, $new, $file);

虽然将术语表中的对加载到 2 个单独的编号数组中会更有效,然后只需调用 str_replace 一次。

Why are you opening file2.html for reading and writing, then grabbing the contents of it?

(BTW - this is going to go horribly wrong on a system with strict locking)

foreach($glossArray as $value)
{
  $results = str_replace($glossArray,$value ,$file);

I think this should be

foreach($glossArray as $old=>$new)
{
   $results = str_replace($old, $new, $file);

Although it would be a lot more efficient to load the pairs from the glossary into 2 seperate numbered arrays, then just call str_replace once.

爱已欠费 2024-10-08 07:33:56

str_replace 的第一个参数不应该是 $glossArray 因为它是一个数组而不是要替换的字符串。

我假设您的 CSV 文件包含类似“SEARCH;REPLACE”的内容?在这种情况下,您的 foreach 应如下所示:foreach ($glossArray as $searchString => $value)

然后尝试

$file = str_replace($searchString, $value ,$file);

而不是

$results = str_replace($searchString, $value ,$file);

因为正确现在,完成后,您将在每次 str_replace ... echo $file 时一次又一次地覆盖 $results

顺便说一句:$counter 在做什么?

Your first parameter for str_replace should not be $glossArray as that's an array and not the string to replace.

I assume that your CSV-file contains something like "SEARCH;REPLACE"? In that case, your foreach should look like this: foreach ($glossArray as $searchString => $value).

Then try

$file = str_replace($searchString, $value ,$file);

instead of

$results = str_replace($searchString, $value ,$file);

because right now you're overwriting $results again and again with every str_replace ... echo $file when you're done.

BTW: What's $counter doing?

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