2012-04-12 2 views
0

동적 HTML 테이블에서 CSV로 데이터를 내보내고 있습니다.PHP 스크립트 내에서 텍스트를 UTF-8로 변환

그러나이 때로는 데이터가 제어 문자 등을 가지고 있기 때문에 몇 가지 문제가 발생

내가 필요로하는 모든 밖으로 제거 또는 가능하면 '친절한'했다?

누구에게 도움이 될지 모르겠다. 여기

내 스크립트입니다

내가 제대로 질문을 이해하지만, 경우
<textarea name="siteurl" rows="10" cols="50"> 
<?php //Check if the form has already been submitted and if this is the case, display the  submitted content. If not, display 'http://'. 
echo (isset($_GET['siteurl']))?htmlspecialchars($_GET['siteurl']):"http://";?> 
</textarea><br> 
<input type="submit" value="Submit"> 
</form> 
</div> 
<div id="nofloat"></div> 
<table class="metadata" id="metatable_1"> 
<?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>"; 
    } 
} 
?> 
</table> 
<script type="text/javascript"> 
     var exportTable1=new ExportHTMLTable('metatable_1'); 
    </script> 
<div> 
     <input type="button" onclick="exportTable1.exportToCSV()" value="Export to CSV"/> 
     <input type="button" onclick="exportTable1.exportToXML()"  value="Export to XML"/> 
    </div> 

</body> 
+0

'utf8_encode()'함수를 사용해 보셨습니까? – sed

+0

@Qmal 나는 그것을 보았다. 그러나 어떻게 이것을 내 대본에 넣을 수 있을까? – RuFFCuT

답변

1

확실하지 당신이 원하는 모든는 UTF-8은 당신이 당신이 작성하고있는 데이터에 utf8_encode()를 사용할 수, CSV 인코딩 된 경우 파일에.

제어 문자를 생략하려면 제어 문자 행을 확인한 후 ctype_cntrl() ...을 사용하여 파일에 기록한 다음 일반 표현식을 사용하여 제거하거나 거부하십시오 줄을 모두 써라. 잘못 표시하고, 그 $tags['description']입니다 무엇을 텍스트입니다 지정하십시오
echo "<tr><td>Description($url)</td><td>" . utf8_encode($tags['description']) . "</td></tr>";

:

관련 문제