2012-04-13 85 views
2

我有一個腳本(粘貼在下面),從Url列表中提取元描述,然後我有一個導出功能,將該數據導出爲CSV格式。iconv()或utf8_encode如何讓我的腳本UTF8友好?

唯一的問題是,當有像例如控制字符的UTF-8集以外的字符我導出功能停止 -

什麼是要麼刪除這些字符或替換它們,所以它們是友好的最好方法?而且我怎麼把這個放到我的腳本中呢?

<?php 
error_reporting(E_ALL); 
//ini_set("display_errors", 0); 
function parseUrl($url){ 
    //Trim whitespace of the url to ensure proper checking. 
    $url = trim($url); 
    //Check if a protocol is specified at the beginning of the url. If it's not, prepend 'http://'. 
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) { 
      $url = "http://" . $url; 
    } 
    //Check if '/' is present at the end of the url. If not, append '/'. 
    if (substr($url, -1)!=="/"){ 
      $url .= "/"; 
    } 
    //Return the processed url. 
    return $url; 
} 
//If the form was submitted 
if(isset($_GET['siteurl'])){ 
    //Put every new line as a new entry in the array 
    $urls = explode("\n",trim($_GET["siteurl"])); 
    //Iterate through urls 
    foreach ($urls as $url) { 
      //Parse the url to add 'http://' at the beginning or '/' at the end if not already there, to avoid errors with the get_meta_tags function 
      $url = parseUrl($url); 
      //Get the meta data for each url 
      $tags = get_meta_tags($url); 
      //Check to see if the description tag was present and adjust output accordingly 
      $tags = NULL; 
$tags = get_meta_tags($url); 
if($tags) 
echo "<tr><td>Description($url)</td><td>" .$tags['description']. "</td></tr>"; 
else 
echo "<tr><td>Description($url)</td><td>No Meta Description</td></tr>"; 
    } 
} 
?> 

出口代碼:

// ExportHTMLTable 

function ExportHTMLTable(tableId) 
{ 
    this.data=[]; 
    this.table=''; 
    this.tableId=tableId; 

    this.formId='exportForm'; 

    this.configuration={url:'../export.php',dataType:'json'}; 

    this.blockSend=0; 
    this.blockSize=100024; 

    this.requestNumber=0; 

    this.rowLastIndex=0; 
    this.cellLastIndex=0; 

    this.format=''; 

    // Export 
    this.exportDocument=function() 
    { 
     this.reset(); 
     if(!this.prepareData()) return(false); 

     this.send(); 
    }  

    // Export to CSV 
    this.exportToCSV=function() 
    { 
     this.format='csv'; 
     this.exportDocument(); 
    } 

    // Export to XML 
    this.exportToXML=function() 
    { 
     this.format='xml'; 
     this.exportDocument(); 
    } 

    // Reset variables 
    this.reset=function() 
    { 
     this.data=[]; 

     this.blockSend=0; 
     this.rowLastIndex=0; 
     this.cellLastIndex=0;  
     this.requestNumber=0; 
    } 

    // Get table 
    this.getTable=function() 
    { 
     this.table=$('#'+this.tableId); 
     if(this.table.length!=1) return(false); 

     return(true); 
    } 

    // Get data length 
    this.getDataLength=function() 
    { 
     var sum=0; 
     for(var rows in this.data) 
      sum+=this.data[rows].length; 

     return(sum); 
    } 

    // Remove form 
    this.removeForm=function() 
    { 
     var form=$('#'+this.formId); 
     if(form.length==1) form.remove(); 
    } 

    // Create field 
    this.createField=function(name,value) 
    { 
     var field=document.createElement('input'); 

     field.name=name; 
     field.value=value; 
     field.type='hidden'; 

     return($(field)); 
    } 

    // Prepare (extract from HTML table) data 
    this.prepareData=function() 
    { 
     if(!this.getTable()) return(false); 

     var self=this; 
     var r=0,c=0,tr=0,tc=0,length=0; 

     // Processing rows 
     this.table.children('tbody').children('tr').each(function() 
     { 
      c=0; 
      if(!$.isArray(self.data[r])) self.data[r]=[]; 

      // Processing cells 
      $(this).children('th,td').each(function() 
      {  
       length=$(this).attr('colspan')+self.data[r].length; 

       for(tc=0;tc<length;tc++) 
       { 
        if(self.data[r][tc]) continue; 
        self.data[r][tc]=$(this).text();  
       } 

       length=r+$(this).attr('rowspan'); 
       for(tr=r+1;tr<length;tr++) 
       { 
        if(!$.isArray(self.data[tr])) self.data[tr]=[]; 
        self.data[tr][c]=$(this).text(); 
       } 

       c++; 
      }); 

      r++; 
     }); 

     return(true);   
    } 

    // Send data 
    this.send=function() 
    { 
     var i=0,j=0; 

     this.requestNumber++; 

     this.removeForm(); 

     var rowsNumber=this.data.length; 
     var form=$(document.createElement('form')); 

     form.attr('method','post'); 
     form.attr('action',this.configuration.url); 

     for(i=this.rowLastIndex;i<rowsNumber;i++) 
     { 
      var cellsNumber=this.data[i].length; 
      for(j=this.cellLastIndex;j<cellsNumber;j++) 
      { 
       this.blockSend++; 

       var field=this.createField('exportTable['+i+']['+j+']',this.data[i][j]); 

       form.append(field); 

       if(this.blockSend>=(this.requestNumber*this.blockSize)) break; 
      } 

      if(this.blockSend>=(this.requestNumber*this.blockSize)) break; 
      this.cellLastIndex=0; 
     } 

     this.rowLastIndex=i; 
     this.cellLastIndex=j==0 ? 0 : j+1; 

     form.appendTo('body'); 

     if(this.requestNumber==1) 
      form.append(this.createField('requestFirst',1)); 

     if(this.blockSend==this.getDataLength()) 
     {  
      form.append(this.createField('requestLast',1)); 
      form.append(this.createField('format',this.format)); 
      form.submit().remove(); 
     } 
     else $.post(this.configuration.url,form.serialize(),$.delegate(this.send,this),'json'); 
    } 
} 
+0

您是否也可以使用導致問題的確切行來顯示導出功能? – Aurimas 2012-04-13 09:05:01

+0

導出功能跨越幾個,但我會張貼我認爲你需要:) – RuFFCuT 2012-04-13 09:15:37

+0

@Aurimas確定添加代碼! – RuFFCuT 2012-04-13 09:17:01

回答

0

我的媽呀,有很多東西不對您的發帖..

第一: - 不被認爲是控制字符。它出現在UTF-8,latin1甚至ASCII中。

如果您的字符串中確實存在無效的UTF-8字節,那麼您必須處理其他一些編碼。這是最有可能的,這是latin1,在這種情況下,你可以使用utf8_encode來轉換字符串。

不要盲目做這件事,首先找出你的數據來自哪裏,如果它真的拉丁文1。有時候,utf8_encode被用作任何奇怪行爲的創可貼,但這不是處理事情的好方法。作爲一項規則,它可能會幫助想想這樣說:

  • 在你的應用程序,確保每總是傳來傳爲UTF-8。
  • 如果您從外部來源接收數據,請確定它們使用的是什麼字符集,並在需要時進行轉換。

我不知道這是如何與您發佈的代碼段相關的。

+0

@Everts我很抱歉大聲笑我不是一個像你這樣的編碼親...我認爲這是代碼,取得我需要改變的數據,所以它轉換它,所以我可以成功導出它?是否沒有辦法在打印之前刪除導致問題的字符?我看不到他們使用什麼字符,因爲它每次都會來自不同的來源。 – RuFFCuT 2012-04-13 09:12:35