簡體   English   中英

去除除 src 之外的所有 HTML 屬性

[英]Strip all HTML attributes except for src

我正在嘗試刪除除src屬性之外的所有標簽屬性。 例如:

<p id="paragraph" class="green">This is a paragraph with an image <img src="/path/to/image.jpg" width="50" height="75"/></p>

將返回為:

<p>This is a paragraph with an image <img src="/path/to/image.jpg" /></p>

我有一個正則表達式來去除所有屬性,但我試圖調整它以保留在src 這是我到目前為止所擁有的:

<?php preg_replace('/<([A-Z][A-Z0-9]*)(\b[^>]*)>/i', '<$1>', '<html><goes><here>');

這可能適合您的需求:

$text = '<p id="paragraph" class="green">This is a paragraph with an image <img src="/path/to/image.jpg" width="50" height="75"/></p>';

echo preg_replace("/<([a-z][a-z0-9]*)(?:[^>]*(\ssrc=['\"][^'\"]*['\"]))?[^>]*?(\/?)>/i",'<$1$2$3>', $text);

// <p>This is a paragraph with an image <img src="/path/to/image.jpg"/></p>

RegExp 分解:

/              # Start Pattern
 <             # Match '<' at beginning of tags
 (             # Start Capture Group $1 - Tag Name
  [a-z]         # Match 'a' through 'z'
  [a-z0-9]*     # Match 'a' through 'z' or '0' through '9' zero or more times
 )             # End Capture Group
 (?:           # Start Non-Capture Group
  [^>]*         # Match anything other than '>', Zero or More Times
  (             # Start Capture Group $2 - ' src="...."'
   \s            # Match one whitespace
   src=          # Match 'src='
   ['"]          # Match ' or "
   [^'"]*        # Match anything other than ' or " 
   ['"]          # Match ' or "
  )             # End Capture Group 2
 )?            # End Non-Capture Group, match group zero or one time
 [^>]*?        # Match anything other than '>', Zero or More times, not-greedy (wont eat the /)
 (\/?)         # Capture Group $3 - '/' if it is there
 >             # Match '>'
/i            # End Pattern - Case Insensitive

添加一些引用,並使用替換文本<$1$2$3>它應該從格式良好的 HTML 標簽中去除任何非src=屬性。

請注意這不一定適用於所有輸入,因為 Anti-HTML + RegExp 人員在下面非常聰明地注意到。 有一些后備,最明顯的是<p style=">">最終會成為<p>">和其他一些損壞的問題......我建議將Zend_Filter_StripTags視為 PHP 中的完整證明標簽/屬性過濾器

通常不會解析HTML應該使用正則表達式

相反,您應該調用DOMDocument::loadHTML
然后您可以遞歸遍歷文檔中的元素並調用removeAttribute

好的,這是我使用的似乎運行良好的方法:

<([A-Z][A-Z0-9]*)(\b[^>src]*)(src\=[\'|"|\s]?[^\'][^"][^\s]*[\'|"|\s]?)?(\b[^>]*)>

隨意戳它的任何孔。

不幸的是,我不確定如何為 PHP 回答這個問題。 如果我使用 Perl,我會執行以下操作:

use strict;
my $data = q^<p id="paragraph" class="green">This is a paragraph with an image <img src="/path/to/image.jpg" width="50" height="75"/></p>^;

$data =~ s{
    <([^/> ]+)([^>]+)> # split into tagtype, attribs
}{
    my $attribs = $2;
    my @parts = split( /\s+/, $attribs ); # separate by whitespace
    @parts = grep { m/^src=/i } @parts;   # retain just src tags
    if ( @parts ) {
        "<" . join( " ", $1, @parts ) . ">";
    } else {
        "<" . $1 . ">";
    }
}xseg;

print( $data );

返回

<p>This is a paragraph with an image <img src="/path/to/image.jpg"></p>

發布為Oracle Regex提供解決方案

<([^!][a-z][a-z0-9]*)([^>]*(\ssrc=[''''\"][^''''\"]*[''''\"]))?[^>]*?(\/?)>

不要使用正則表達式來解析有效的 html。 僅當所有可用的 DOM 解析器都失敗時,才使用正則表達式來解析 html 文檔。 我超級喜歡正則表達式,但正則表達式是“DOM-ignorant”,它會悄悄地失敗和/或改變你的文檔。

我通常更喜歡 DOMDocument 和 XPath 的混合,以簡潔、直接和直觀地定位文檔實體。

除了少數幾個小例外,XPath 表達式與簡單英語中的邏輯非常相似。

//@*[not(name()="src")]

  • 在文檔中的任何級別 ( // )
  • 查找任何屬性( @*
  • 滿足這些要求 ( [] )
  • 那不是( not()
  • 命名為“src”( name()="src"

這更具可讀性、吸引力、廣告可維護性。

代碼:(演示

$html = <<<HTML
<p id="paragraph" class="green">
    This is a paragraph with an image <img src="/path/to/image.jpg" width="50" height="75"/>
</p>
HTML;

$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//@*[not(name()="src")]') as $attr) {
    $attr->parentNode->removeAttribute($attr->nodeName);
}
echo $dom->saveHTML();

輸出:

<p>
    This is a paragraph with an image <img src="/path/to/image.jpg">
</p>

如果要添加另一個豁免屬性,可以使用or

//@*[not(name()="src" or name()="href")]

如上所述,您應該使用正則表達式來解析 html 或 xml。

我會用 str_replace() 來做你的例子; 如果它的所有時間都一樣。

$str = '<p id="paragraph" class="green">This is a paragraph with an image <img src="/path/to/image.jpg" width="50" height="75"/></p>';

$str = str_replace('id="paragraph" class="green"', "", $str);

$str = str_replace('width="50" height="75"',"",$str);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM