douzhenchun6782 2012-04-12 21:02
浏览 53
已采纳

在PHP脚本中将文本转换为UTF-8

I am exporting data to CSV from a dynamic html table.

However this causes some problems because sometimes the data has control characters etc.

I need all these stripped out or made 'friendly' if possible?

I have no idea how to do this so could anyone help?

Here is my script:

<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("
",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>
  • 写回答

2条回答 默认 最新

  • dousheyan0375 2012-04-13 10:58
    关注

    I'm guessing you want something like:
    echo "<tr><td>Description($url)</td><td>" . utf8_encode($tags['description']) . "</td></tr>";

    Please specify what text is it that is displaying wrong, is it $tags['description']?

    Here are the manuals for function you probably need: mb_convert_encoding, utf8_encode.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法