1

When reading text from word files, I get the following output. Some weird characters are printed out. Is there any way to remove them?

enter image description here

I use this function to read from docx files

function readDocx() {
    // Create new ZIP archive
    $zip = new ZipArchive;
    $dataFile = 'word/document.xml';
    // Open received archive file
    if (true === $zip->open($this->doc_path)) {
        // If done, search for the data file in the archive
        if (($index = $zip->locateName($dataFile)) !== false) {
            // If found, read it to the string
            $data = $zip->getFromIndex($index);
            // Close archive file
            $zip->close();
            // Load XML from a string
            // Skip errors and warnings
            $xml = DOMDocument::loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
            // Return data without XML formatting tags

            $contents = explode('\n',strip_tags($xml->saveXML()));
            $text = '';
            foreach($contents as $i=>$content) {
                $text .= $contents[$i];
            }
            return $text;
        }
        $zip->close();
    }
    // In case of failure return empty string
    return "";
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
SupaOden
  • 722
  • 4
  • 13
  • 41
  • What have you done to try to fix this problem? How has it not worked? – sarnold Dec 24 '11 at 02:20
  • Looks like you have an *encoding* problem you need to fix, not anything that has to do with *escaping*. Give us more details. When does this occur? What characters *should* be there? – deceze Dec 24 '11 at 02:22
  • Could you please elaborate your question? – Lion Dec 24 '11 at 02:22
  • possible duplicate of [weird characters](http://stackoverflow.com/questions/5588112/weird-characters) – mario Dec 24 '11 at 02:23
  • Can you show your code? You we can see what make the weird symbols. If you want to write some symbol like (') or (") and get try to use HTML entities (http://www.w3schools.com/tags/ref_entities.asp). One more note that you should check your file encoding! – nvcnvn Dec 24 '11 at 02:23
  • This is what I get when I read text from docxfile – SupaOden Dec 24 '11 at 02:24
  • 1
    Are you reading a [`docx` file](http://en.wikipedia.org/wiki/Docx) directly and hoping that its output is understood by browsers? – sarnold Dec 24 '11 at 02:26
  • I just need the text, I dont need to preserve formatting. – SupaOden Dec 24 '11 at 02:28

2 Answers2

1

This is the part I love most:

        $contents = explode('\n',strip_tags($xml->saveXML()));
        $text = '';
        foreach($contents as $i=>$content) {
            $text .= $contents[$i];
        }
        return $text;

No idea where you copied it from, but it's basically:

        $text = strip_tags($xml->saveXML());
        return $text;

Next to that, saveXML() returns a string in UTF-8 encoding. Your browser expects something else, so just change the encoding to that something (you should know it).

As I don't know what is probably unknown to you as well, just wrap anything into HTML entities to make this dead-safe:

        $text = strip_tags($xml->saveXML());
        return htmlentities($text, ENT_QUOTES, 'UTF-8');

The real fix actually would be that you understand what you are sending to the browser and then tell the browser what it is.

hakre
  • 193,403
  • 52
  • 435
  • 836
0

This has nothing to do with php... It's a server encoding issue. Look at apache's default encoding setting.

Ben D
  • 14,321
  • 3
  • 45
  • 59