PHP функция для преобразования HTML-кодов в обычные символы - PullRequest
1 голос
/ 13 февраля 2012

У меня есть такая строка:

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

Ответы [ 2 ]

6 голосов
/ 13 февраля 2012

html_entity_decode .Из руководства php.net: html_entity_decode () является противоположностью htmlentities () в том смысле, что он преобразует все сущности HTML в строке в соответствующие символы.

2 голосов
/ 13 февраля 2012

попробуйте это

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');
...