Reputation: 107
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.
Upvotes: 0
Views: 1003
Reputation: 2563
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.
Upvotes: 1