Jump to content


Photo

php xml utf8 encoding

xml encoding utf8

Best Answer DjordjeB, 05 November 2013 - 06:18 PM

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)

 

Go to the full post


  • Please log in to reply
4 replies to this topic

#1 DjordjeB

DjordjeB

    Newbie

  • New Members
  • Pip
  • 4 posts

Posted 04 November 2013 - 07:08 PM

$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?



#2 requinix

requinix

    Hopeless Member

  • Moderators
  • 5,914 posts
  • LocationWA

Posted 04 November 2013 - 08:24 PM

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.

#3 DjordjeB

DjordjeB

    Newbie

  • New Members
  • Pip
  • 4 posts

Posted 05 November 2013 - 05:58 AM

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?


#4 requinix

requinix

    Hopeless Member

  • Moderators
  • 5,914 posts
  • LocationWA

Posted 05 November 2013 - 02:03 PM

Your data is not in UTF-8. If it were then you wouldn't be having this problem.

As a guess, try ISO 8859-5.
$dodajime = $doc->createTextNode(mb_convert_encoding($pod['Name'],"UTF-8","ISO-8859-5"));


#5 DjordjeB

DjordjeB

    Newbie

  • New Members
  • Pip
  • 4 posts

Posted 05 November 2013 - 06:18 PM   Best Answer

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)

 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com