Jump to content

php xml utf8 encoding


Go to solution Solved by DjordjeB,

Recommended Posts

$doc = new DOMDocument(); 
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$doc->load("proba.xml");
  
$root = $doc->documentElement;
$filmovi=mysql_query("SELECT * FROM film ORDER BY film_id DESC LIMIT 0,100");
while($pod=mysql_fetch_array($filmovi))
{
$film = $doc->createElement('film');
$film = $root->appendChild($film);

$link = $doc->createElement('link');
$link = $film->appendChild($link);
$dodajlink = $doc->createTextNode('film-'.seoURL($pod['film_ime']."_".$pod['film_imesrpski']).'-'.$pod['film_id']);
$dodajlink = $link->appendChild($dodajlink);

$ime = $doc->createElement('ime');
$ime = $film->appendChild($ime);
$dodajime = $doc->createTextNode(utf8_encode($pod['film_ime'].' '.$pod['film_imesrpski'].' '.$pod['film_imestrano']));
$dodajime = $ime->appendChild($dodajime);

$doc->save("proba.xml"); }

thet was proba.php

 

proba.xml is: 

 

<?xml version="1.0" encoding="UTF-8"?>
<pretraga>
  <film>
    <link>film-Rough_Magic_Opasna_magija-4657</link>
    <ime>Rough Magic Opasna magija </ime>
  </film>
  <film>
    <link>film-HouseSitter_uvarkua-4656</link>
    <ime>HouseSitter ?uvarku?a </ime>
  </film></pretraga>

and livesearch.php

 

$xmlDoc=new DOMDocument();
$xmlDoc->load("proba.xml");

$x=$xmlDoc->getElementsByTagName('film');

//get the q parameter from URL
$q=$_GET["q"];

//lookup all links from the xml file if length of q>0
if (strlen($q)>0)
{
$hint="";
for($i=0; $i<($x->length); $i++)
  {
  $y=iconv('','UTF-8',$x->item($i)->getElementsByTagName('ime'));
  $z=$x->item($i)->getElementsByTagName('link');
  if ($y->item(0)->nodeType==1)
    {
    //find a link matching the search text
    if (strpos($y->item(0)->childNodes->item(0)->nodeValue,$q))
      {
      if ($hint=="")
        {
        $hint="<a href='".$z->item(0)->childNodes->item(0)->nodeValue . 
        "' target='_blank'>" . 
        $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        }
      else
        {
        $hint=$hint . "<br /><a href='".$z->item(0)->childNodes->item(0)->nodeValue . 
        "' target='_blank'>" . 
        $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        }
      }
    }
  }
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint=="")
  {
  $response="Nema slicnih filmova";
  }
else
  {
  $response=$hint;
  }
//output the response
echo $response;

 

livesearch and all work great except if i use non-english characters... i try with utf8_encoding but doesent work, i read a lot of diffrient things but i don't know how to use function like: iconv

 

livesearch script

 

When i try to add non-english charcter in xml file and when i use search i got this message:

Warning: DOMDocument::load() [domdocument.load]: Input is not proper UTF-8, indicate encoding ! Bytes: 0x9A 0x61 0x72 0x6B in

 

 

how can i fix this problem?

Link to comment
https://forums.phpfreaks.com/topic/283600-php-xml-utf8-encoding/
Share on other sites

utf8_encode() only converts from ISO 8859-1/Latin1 (which I don't think you're using) to UTF-8.

 

If you have the mbstring extension, I like that better than iconv.

$output = mb_convert_encoding($input, "UTF-8", "whatever your input charset is");
Otherwise

$output = iconv("whatever your input charset is", "UTF-8", $input);
The real question is what your current character encoding is.

mysql is utf-8 script you see. I need to write utf8 in xml.
 

$ime = $doc->createElement('ime');
$ime = $film->appendChild($ime);
$dodajime = $doc->createTextNode(mb_convert_encoding($pod['Name'],"UTF-8","UTF-8"));
$dodajime = $ime->appendChild($dodajime);
 
with this i don't have any error on proba.php (first page) but when i open xml i got this
This page contains the following errors:
error on line 3 at column 4625: Encoding error

and line with this: Bellflower Zvon?i?
  • Solution

i solve problem with mysql_query("set names 'utf8'"); and write xml with mb_convert_encoding utf8 utf8 and works greet, but now i have another problem with search xml,

do you know better solution for search then: if (strpos(mb_strtolower($y->item(0)->childNodes->item(0)->nodeValue),$q)

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.