0

so I have been trying to wrap my head around this thing few hours, but nothing seems to work as expected.

for($i = 0 ; $i < count($lines); $i++){ $currentLine = $lines[$i]; $search = array('ø', '', 'ï', 'ò');$replace = array('ř', 'ť', 'ď', 'ň'); $subject = str_replace($search, $replace, $currentLine); $finalLine = '"'.$subject.'",'; file_put_contents($fileName, $finalLine, FILE_APPEND); };

The thing is simple, I am iterating over lines in .dic file (it's just list of words, one word at a line), for each word I do some replacing of wrong characters and at the end of all I try to wrap the word in double quotes (single quotes don't work either).

The problem is, that the quotes aren't added correctly, and instead of "someword", i get ","someword

// $currentLine = someword; echo '"'.$currentLine.'",'; // returns ","someword

I need this to convert .dic file to json so i can use it in javascript game i am making.

Can someone please help? I can't find any similar question, not here, not on google, nothing. I tried single quotes, i tried making the whole thing array

$newline = Array( 0 => '"', 1 => $currentLine, 2 => '",' )

and then joining the array but with the same result, I am kinda desperate now, so if someone knows what am I doing wrong, please tell me.

6

1 Answer 1

1

I can't tell from the code why you are getting that result. But here is an alternative approach that might help.

// Set these outside the loop $search = array('ø', '', 'ï', 'ò'); $replace = array('ř', 'ť', 'ď', 'ň'); $subjects = array(); foreach ($lines as $line) { $subject = str_replace($search, $replace, $line); $subject = trim($subject); // trim if you want to remove extraneous spaces if (!empty($subject)) // Presumably you don't want to add empty lines if there are any { $subjects[] = $subject; } } $allwords = implode(',', $subjects); file_put_contents($fileName, $allwords, FILE_APPEND);

Essentially, create an array of the words, join them with "," using the implode() library function and then add the whole lot to your file.

2 Comments

Yes that works, but still, i need those words in quotes, because there are some words like "key" "default" "break" and so on, and when i import this file into javascript, it breaks on those words. But thanks, this is still better than what I did. Update: Got it joined with '","', now i just need to add the stating and ending quote. Thanks @Greg.
Sorry, I forgot about the quotes but great that you got them added.

Your Answer

Draft saved
Draft discarded

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.