3

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.

3
  • That code sample does look like it would parse. Something seems to be missing from line 9 or 10. Commented Nov 5, 2010 at 14:26
  • 1
    +1 Why a downvote? OP has tried to solve this on his own and is showing his work to this point. Commented Nov 5, 2010 at 14:29
  • You can't concatenate a string to the product of array_values($glossArray). You're concatenating a string with an array so you get undesired behavior. You should concatenate the "means ". string when you do = "means ".$line_of_text[1]; earlier on. Commented Nov 5, 2010 at 15:40

5 Answers 5

4

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);
Sign up to request clarification or add additional context in comments.

4 Comments

+1 for the array_keys/array_values str_replace.... although the OP could build a from and a to array in his initial loop through the CSV
that was perfect .. but the problem is that it adds the word Array with each replacement ..
@user498401: It looks like in your edited question you're doing this: >> $results = "cioe ".array_values($glossArray) << You can't concatenate a string to the array_values because array_values is an array. You should remove the "cioe ". and place that at the assignment here: $glossArray[$line_of_text[0]] = "cioe ".$line_of_text[1];
yes .. after taking that out it worked just fine .. thank you very much.
4

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!

1 Comment

thank you for this .. i understand better how it works .. but still the problem persists ...
3

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.

Comments

1

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?

3 Comments

Try var_dump($glossArray) after you read the CSV-file to make sure your glossary looks like it should.
tried it .. gives me key => value pairs ... seems like it works fine.
In your edited code you still overwrite $results which each str_replace!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.