【问题标题】:Clean special character in html using php使用 php 清除 html 中的特殊字符
【发布时间】:2016-01-28 16:00:07
【问题描述】:

我在 php 中使用goutte 来获取页面的 html。我使用jquery ajax 调用php,并将页面放在文档区域(#doc)。

我想把那个页面不带特殊字符,比如  和其他字符,但是我的clean() 函数不起作用。我该如何解决?

PHP:

<?php
require_once 'goutte.phar';
use Goutte\Client;

if(isset($_GET['url'])) {
  $url = $_GET['url'];
}
//client used to send requests to a website and returns a crawler object
$client = new Client();
$client->getClient()->setDefaultOption('config/curl/'.CURLOPT_SSL_VERIFYHOST, FALSE); //codice per accettare anche https
$client->getClient()->setDefaultOption('config/curl/'.CURLOPT_SSL_VERIFYPEER, FALSE);
$crawler = $client->request('GET', $url);
if($status_code==200){



        $result = $crawler->filterXPath('html/body')->html(); 
        $result=clean($result);
        echo $result;

}
else {
    //in case of error
    echo "HTTP/1.0 400 Bad Request";
}

function clean($conv) {
    $string = htmlentities($conv, null, 'utf-8');
    $conv = str_replace("&nbsp;", "", $string);
    $conv = html_entity_decode($conv);
    return($conv);
}

?>

JAVASCRIPT:

function visual(search) {


    $.ajax({
            type: "GET",
            url: "goutte.php?url="+search,
            success: function(data)
            {
                var content=$.parseHTML(data);
                $("#doc").html(contenuto);

            },
            //azione in caso di errore
            error: function()
            {
                alert("Error");
            }
        });
}

【问题讨论】:

    标签: php jquery ajax goutte


    【解决方案1】:

    如果要将编码后的 html 解码回常规,则需要使用html_entity_decode。这就是你需要做的。在 html 编码的字符串上再次使用 htmlentities 是错误的,以及使用 str_replace。

    因此,您的 clean 函数应该只解码 html 编码的字符串。

    function clean($conv) {
        $conv = html_entity_decode($conv, NULL, "UTF-8"); //To 'force' UTF-8 charset (php.ini settings may differ, that's why!)
        return $conv;
    }
    

    http://php.net/html_entity_decode

    【讨论】:

    • 它不起作用,在 $("#doc") 中,我可以看到使用 string.charCodeAt(index),像 10 这样的代码是我不想拥有的字符跨度>
    • 添加一些replace来照顾这些角色?
    • 将$crawler-&gt;...的输出和clean的输出转储为var_dump(),如果输出仍然是html编码,那么你的输入已经不止一次被html编码(或者输入已损坏)。
    • $conv = str_replace(chr(10),'',$conv);
    • @Veve 我应该在哪里添加替换?在我的功能或夏洛特功能?解码前还是解码后?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    相关资源
    最近更新 更多