Search code examples
phpms-wordconvertersspecial-charactershtmlspecialchars

Special character/ Unicode / special symbol to word from php


I'm using COM for generate word document from PHP. i have a big issue when convert character for send to word bookmark template generating useless Chinese character and some number.

here is what i have string value in php - Brüel & Kjær

this is the text of word document after generate - Brüel & Kjær1墄

here is my converting way in php:

$text="Brüel & Kjær";
$text = iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8",$text);
$text = htmlspecialchars_decode($text);
$text=html_entity_decode($text, ENT_QUOTES, 'utf-8');

this is how adding COM bookmark variable to value :

$PHPWord = new COM("word.application", NULL, CP_UTF8);
$PHPWord->Documents->Open(realpath($template));
$bookmark = $PHPWord->ActiveDocument->Bookmarks("bookmark1");
$bookmark = $bookmark->Range;
$bookmark->Text=$text;
$PHPWord->Documents[1]->SaveAs('temp/' . $template);

i beleve the character converting issue in my code. but i searched on internet, i couldn't found working solution for my problem. can you please help to solve this.

is their way to send html to this bookmark or any convert ??


Solution

  • OMG almost done with this :

    $text="Brüel & Kjær";
    
    $PHPWord = new COM("word.application", NULL, CP_UTF8);
    

    replaced to :

    $PHPWord = new COM("word.application");
    $PHPWord->Documents->Open(realpath($template));
    $bookmark = $PHPWord->ActiveDocument->Bookmarks("bookmark1");
    $bookmark = $bookmark->Range;
    $bookmark->Text=html_entity_decode($text);
    $PHPWord->Documents[1]->SaveAs('temp/' . $template);