删除 PHP 中 HTML 的所有样式属性
Remove all style attributes from HTML in PHP
我必须加载 HTML 页面的正文,没有任何样式属性,也没有 link 图像和所有非“纯文本”的内容。我想在 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");
}
});
});
您可以使用 jQuery 来获取 body
的文本内容。
因此,在您的 success
函数中,您将获取 data
,将其转换为 jQuery 对象并将文本插入 div。
$('div#risultato').html($(data).find('body').text());
您可以在插入 body
:
后逐个标记地清除 style
属性
function clearStyles(element) {
element.setAttribute('style', '');
for (var i = 0; i < element.children.length; i++) {
clearStyles(element.children[i]);
}
}
clearStyles(document.body);
或者直接用jQuery:
jQuery('body *').attr('style', '');
我更正了 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]);
}
}
我必须加载 HTML 页面的正文,没有任何样式属性,也没有 link 图像和所有非“纯文本”的内容。我想在 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");
}
});
});
您可以使用 jQuery 来获取 body
的文本内容。
因此,在您的 success
函数中,您将获取 data
,将其转换为 jQuery 对象并将文本插入 div。
$('div#risultato').html($(data).find('body').text());
您可以在插入 body
:
style
属性
function clearStyles(element) {
element.setAttribute('style', '');
for (var i = 0; i < element.children.length; i++) {
clearStyles(element.children[i]);
}
}
clearStyles(document.body);
或者直接用jQuery:
jQuery('body *').attr('style', '');
我更正了 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]);
}
}