PHP function to convert from html codes to normal chars

 ̄綄美尐妖づ 提交于 2019-12-11 11:48:38

问题


I have a string like this:

La Torre Eiffel paragonata all’Everest

What PHP function should I use to convert the ’ to the actual "normal" char ':

La Torre Eiffel paragonata all’Everest

I'm using CURL to fetch a page and this page has that string in it but for some reason the HTML chars are not decoded.

The my_url test page is an Italian blog with iso characters, and all the apostrophes are encoded in html code like above.

$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;
}

回答1:


html_entity_decode. From the php.net manual: html_entity_decode() is the opposite of htmlentities() in that it converts all HTML entities in the string to their applicable characters.




回答2:


try this

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

so in your code change this

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

to

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


来源:https://stackoverflow.com/questions/9255531/php-function-to-convert-from-html-codes-to-normal-chars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!