Question

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.

Was it helpful?

Solution

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);

OTHER TIPS

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!

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.

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?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top