【问题标题】:PHP function to convert from html codes to normal charsPHP函数将html代码转换为普通字符
【发布时间】:2012-02-13 04:14:40
【问题描述】:

我有一个这样的字符串:

La Torre Eiffel paragonata all’Everest

我应该使用什么 PHP 函数将 ’ 转换为实际的“普通”字符 '

La Torre Eiffel paragonata all'Everest

我正在使用 CURL 来获取一个页面,并且该页面中包含该字符串,但由于某种原因,HTML 字符没有被解码。

my_url 测试页面是一个带有 iso 字符的意大利博客,所有的撇号都是用上面的 html 代码编码的。

$output = curl_download($my_url);
$output = htmlspecialchars_decode($output);

function curl_download($Url){

    // is cURL installed yet?
    if (!function_exists('curl_init')){
        die('Sorry cURL is not installed!');
    }

    // OK cool - then let's create a new cURL resource handle
    $ch = curl_init();

    // Now set some options (most are optional)

    // Set URL to download
    curl_setopt($ch, CURLOPT_URL, $Url);

    // Set a referer
    curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");

    // User agent
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");

    // Include header in result? (0 = yes, 1 = no)
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // Should cURL return or print out the data? (true = return, false = print)
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    // Download the given URL, and return output
    $output = curl_exec($ch);

    // Close the cURL resource, and free system resources
    curl_close($ch);

    return $output;
}

【问题讨论】:

    标签: php curl decode html-entities


    【解决方案1】:

    html_entity_decode。来自 php.net 手册: html_entity_decode() 与 htmlentities() 相反,它将字符串中的所有 HTML 实体转换为其适用的字符。

    【讨论】:

    • 我在您的示例中看到 htmlspecialchars_decode,而不是 html_entity_decode。
    【解决方案2】:

    试试这个

    echo html_entity_decode('La Torre Eiffel paragonata all’Everest',ENT_QUOTES,'UTF-8');
    

    所以在你的代码中改变这个

     $output = curl_download($my_url);
     $output = htmlspecialchars_decode($output);
    

    $output = curl_download($my_url);
    $output = html_entity_decode($output,ENT_QUOTES,'UTF-8');
    

    【讨论】:

      猜你喜欢
      • 2014-08-17
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多