2017-04-04 82 views
0

我有一個包含(n)HTML頁面的字符串。我只需要< body>和</body>中的內容,並且想要移除除colspan之外的所有內聯HTML屬性。以下是我所取得的成績(仍刪除了colspan特性):正則表達式刪除除colspan(PHP)以外的所有內聯HTML屬性

<?php 
$html = 'CURL GET THE HTML (mostly just tables)'; 

// Remove HTML comments, JavaScript content, CSS and not needed HTML tags 
$pregReplacePattern = array(
    '/<!--(.*)-->/Uis', 
    '#<.*?!DOCTYPE.*?>#i', 
    '#<.*?html.*?>#i', 
    '#<.*?head.*?>#i', 
    '#<title.*?>.*?</title>#i', 
    '#<.*?meta.*?>#i', 
    '#<script.*?>.*?</script#i', 
    '#<.*?link.*?>#i', 
    '#<.*?body.*?>#i', 
    '#<.*?form.*?>#i', 
    '#<img.*?>#i', 
    '"/<img[^>]+\>/i"', 
); 
$pregReplaceTo = array_fill_keys(
    range(0, count($pregReplacePattern) - 1), '' 
); 
$html = preg_replace($pregReplacePattern, $pregReplaceTo, $html); 

// Remove inline HTML properties (all of them) 
$html = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i", '<$1$2>', $html); 

你們能幫助我嗎?

在此先感謝...

+0

所以你只需要'

'(S)? –

+0

不完全。我需要身體標籤內的所有東西,但這些東西大多隻是包含大量內聯HTML屬性的表格。我想刪除這些屬性,但colspan除外。 –

回答

-1

也許你應該只使用回聲satements在你的PHP和理會正則表達式。這裏是我的意思是:

<?php 
echo "// Remove HTML comments, JavaScript content, CSS and not needed HTML 
tags 
$pregReplacePattern = array(
'/<!--(.*)-->/Uis', 
'#<.*?!DOCTYPE.*?>#i', 
'#<.*?html.*?>#i', 
'#<.*?head.*?>#i', 
'#<title.*?>.*?</title>#i', 
'#<.*?meta.*?>#i', 
'#<script.*?>.*?</script#i', 
'#<.*?link.*?>#i', 
'#<.*?body.*?>#i', 
'#<.*?form.*?>#i', 
'#<img.*?>#i', 
'"/<img[^>]+\>/i"', 
); 
$pregReplaceTo = array_fill_keys(
range(0, count($pregReplacePattern) - 1), '' 
); 
$html = preg_replace($pregReplacePattern, $pregReplaceTo, $html); 

// Remove inline HTML properties (all of them) 
$html = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i", '<$1$2>', $html);"; 
?> 

它工作正常,在IE 9.1

相關問題