0

I am reading some data from a remote file, got every thing working till the point when i write some specific lines to a text file.
problem here is, when i write something like Girl's Goldtone 'X' CZ Ring it becomes Girl & apos;s Goldtone &apos ;X & apos; CZ Ring in txt file.
how do i write to txt file so that it retains text like written above and not show character code but actual character.
sample of my code.

$content_to_write = '<li class="category-top"><span class="top-span"><a class="category-top" href="'.$linktext.'.html">'.$productName.'</a></span></li>'."\r\n";
                fwrite($fp, $content_to_write);  
$linktext = "Girls-Goldtone-X-CZ-Ring";  
$productName = "Girl's Goldtone 'X' CZ Ring";  

var_dump

string '<li class="category-top"><span class="top-span"><a class="category-top" href="Stellar-Steed-Gallery-wrapped-Canvas-Art.html">&apos;Stellar Steed&apos; Gallery-wrapped Canvas Art</a></span></li>

' (length=195)

Code

                $productName =$linktext;


                $linktext = str_replace(" ", "-", $linktext);
                $delChar = substr($linktext, -1);
                if($delChar == '.')
                {
                    $linktext = substr($linktext, 0, -1);
                }

                $linktext = removeRepeated($linktext);
                $linktext = remove_invalid_char($linktext);
                $productName = html_entity_decode($productName);
                $content_to_write = '<li class="category-top"><span class="top-span"><a class="category-top" href="'.$linktext.'.html">'.$productName.'</a></span></li>'."\r\n";
                var_dump($content_to_write);
                fwrite($fp, utf8_encode($content_to_write));
Muhammad Irfan
  • 131
  • 1
  • 6
  • That's proberblay just a matter of encoding. Which encoding is used in the external file, which is used when viewing the output and which one is used when writing to the .txt file? – Dennis Hunink Mar 11 '12 at 10:23

2 Answers2

2

Is it that you are reading the data from a remote file and then writing the same to a txt file? Agree with the above comment, its an issue with encoding. Try the following code:

$file = file_get_contents("messages.txt");
$file = mb_convert_encoding($file, 'HTML-ENTITIES', "UTF-8");
echo $file;

echo the response to your browser and see. If found proper, write the response to your txt file. Ensure that your txt file is UTF8 - encoded.

Check this out:: Write Special characters in a file.

Community
  • 1
  • 1
verisimilitude
  • 5,077
  • 3
  • 30
  • 35
  • thanks, but i didn't solve the problem. i am reading a txt file from url and then reading a random line via **fgets** this line shows correct on the browser but when i write this line to txt via **fwrite** it messes up all special characters. – Muhammad Irfan Mar 11 '12 at 12:06
1

fwrite is binary-safe, meaning it doesn't do any encoding stuff but just writes whatever you feed it directly to the file. It looks like the $productName variable you're writing is already entity-encoded before writing. Try running html_entity_decode over the variable first.

Note that html_entity_decode doesn't touch single quotes (&apos;) by default; you'll have to set the ENT_QUOTES flag in the second parameter. You might also want to explicitly specify an encoding in the third parameter.

Another Code
  • 3,083
  • 21
  • 23
  • sorry my bad, i corrected $productName. but my variable gets value from an array. which i a random line read from remote file. every thing seems fine on browser but but in txt the problem is still there. – Muhammad Irfan Mar 11 '12 at 12:26
  • yeah, i've been trying for about 2 days now. but nothing seems to work. is there a platform issue or some thing, i am on win 7 64bit, WAMP. does it has to do any thing with that. – Muhammad Irfan Mar 11 '12 at 12:42
  • @MuhammadIrfan: could you show us the actual code you're using plus a `var_dump()` of the data you're writing (just before actually writing it)? – Another Code Mar 11 '12 at 12:50
  • @MuhammadIrfan: check my updated answer, does modifying the quotes mode solve the problem? – Another Code Mar 11 '12 at 13:09
  • still no change, and i can't even see any encoding on the txt file. how do make sure that txt file written with fwrite is encoded. – Muhammad Irfan Mar 11 '12 at 13:21