【问题标题】:Remove all style attributes from HTML in PHP从 PHP 中的 HTML 中删除所有样式属性
【发布时间】:2015-09-03 16:40:38
【问题描述】:

我必须加载没有任何样式属性、没有链接图像以及所有非“纯文本”的 HTML 页面的正文。我想用 PHP 来做,并尝试了非常好的解决方案,但我还没有解决。我使用对我的脚本的 ajax 调用加载 html 页面,然后使用正则表达式获取我想要清除的正文。你能帮助我吗?这是ajax调用:

$.ajax({
       type: "GET"
       url: "core/proxy.php?url="+cerca,              
       success: function(data){
       var body = data.replace(/^[\S\s]*<body[^>]*?>/i, "")
       .replace(/<\/body[\S\s]*$/i, "");
        $("div#risultato").html(body);
    },
      error: function(){
      alert("failed");
    }
    });
});

【问题讨论】:

标签: javascript php html ajax scrape


【解决方案1】:

您可以使用 jQuery 来获取 body 的文本内容。

因此,在您的 success 函数中,您将获取 data,将其转换为 jQuery 对象并将文本插入到您的 div 中。

$('div#risultato').html($(data).find('body').text());

【讨论】:

    【解决方案2】:

    您可以在插入body 后逐个标记清除style 属性:

    function clearStyles(element) {
        element.setAttribute('style', '');
        for (var i = 0; i < element.children.length; i++) {
            clearStyles(element.children[i]);
        }
    }
    

    clearStyles(document.body);

    http://jsfiddle.net/n9ocxa0g/

    或者直接用jQuery:

    jQuery('body *').attr('style', '');
    

    【讨论】:

    • 好的,谢谢并删除链接、 标签和文本框?
    • jQuery('body a, body img, body input, body textarea').remove();
    • 对不起,但是当我的变量主体包含网站主体时,第一个函数 clearStyles 我在 setAttribute 中收到一个错误,Safari 告诉我这不是一个函数,第二个指令不起作用。我将您的函数复制并粘贴到我的 js 文件中,并在我的指令数据替换后调用 clearStyles(body)。
    • 身体如何获得?对于setAttribute(),您不能使用jQuery,您应该使用document.bodydocument.getElementsByTagName('body')[0]
    • 添加一个id到那个div(例如id="content")并调用函数clearStyles(document.getElementById('content'));
    【解决方案3】:

    我更正了 Jose Antonio Riaza Valverde,但没有任何改变:

    $.ajax({
                //definisco il tipo della chiamata
                type: "GET",
                //url della risorsa da contattare
                url: "core/proxy.php?url="+cerca,
                //azione in caso di successo
                success: function(data)
                {
                    var body = data.replace(/^[\S\s]*<body[^>]*?>/i, "")
                    .replace(/<\/body[\S\s]*$/i, "");
                    $("div#risultato").html(body);
                    clearStyles(document.getElementById('risultato'));
    
                },
                //azione in caso di errore
                error: function()
                {
                    alert("Chiamata fallita");
                }
        });
    });
    

    和功能:

    function clearStyles(element) {
    element.setAttribute('style', ' ');
    element.setAttribute('img', ' ');
    element.setAttribute('a', ' ');
    for (var i = 0; i < element.children.length; i++) {
        clearStyles(element.children[i]);
    }
    

    }

    【讨论】:

    • 您尝试过使用 jQuery 吗? jQuery('body *').removeAttr('style');jQuery('img, a').remove();
    • 我已经尝试过,但无事可做。也许我使用 removeAttr 方法是错误的。我用过jQuery('#risultato').removeAttr('style');
    • 您必须使用jQuery('#risultato *').removeAttr('style'); 将其应用到#risultato 的所有子项。
    • 没有错误但无事可做。我在 .html 指令之后使用 jquery
    • 我可以使用 XPATH 从 html 中选择节点主体并应用一个简单的 css 吗?
    猜你喜欢
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 2013-02-02
    相关资源
    最近更新 更多