To capture the encoding from the XML tag of the file. We need to read the first line, and pass it to the function to capture the encoding value.

The first line looks like this:

< ?xml version="1.0" encoding="utf-8"? >

The PHP code:

PHP:
  1. function readfirstline($file){
  2.    $fp = @fopen($file, "r");
  3.    $firstline = fgets($fp);
  4.    fclose($fp);
  5.    return $firstline;
  6. } 
  7. function get_attr(  $line ,$attr) {
  8.   $start = strpos($line,"$attr");
  9.   $linesubstr($line,$start);
  10.   $arr_enc = split("\"",$line);
  11.  
  12.   return $arr_enc [1];
  13. }
  14.  
  15. $path = "home/user/feeds/id.xml";
  16. $line= readfirstline($path);
  17.        
  18. echo get_attr(  $line ,"encoding");