我想從樣式屬性中洗掉除 php 中的 font-family 之外的所有屬性
我試過這個
style=(.*)font-[^;] ;
示例 html
<div style='margin: 0px 14.3906px 0px 28.7969px; padding: 0px; width: 436.797px; float: left; font-family: "Open Sans", Arial, sans-serif;'><p style="margin-right: 0px; margin-bottom: 15px; margin-left: 0px; padding: 0px; text-align: justify;"><strong style="margin: 0px; padding: 0px;">Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><div><br></div></div><div style='margin: 0px 28.7969px 0px 14.3906px; padding: 0px; width: 436.797px; float: right; font-family: "Open Sans", Arial, sans-serif;'></div>
預期產出
<div style='font-family: "Open Sans", Arial, sans-serif;'><p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><div><br></div></div><div style='font-family: "Open Sans", Arial, sans-serif;'></div>
但沒有按預期作業。這有什么需要改變的嗎?
uj5u.com熱心網友回復:
您可能會考慮使用 DOMDocument 來獲取例如來自所有元素的 style 屬性。
如果有樣式,那么您可以使用模式來捕獲捕獲組中的字體系列部分,并在替換中使用該組。
.*?\b(font-[^;] ;?).*|.*
模式匹配:
.*?匹配盡可能少的字符\b(一個字邊界,開始捕獲組1font-[^;] ;?匹配font-然后匹配1 個字符,而不是;后跟一個可選字符;
)關閉第 1 組.*匹配該行的其余部分|.*匹配整行
正則運算式演示
例如
$data = <<<DATA
<div style='margin: 0px 14.3906px 0px 28.7969px; padding: 0px; width: 436.797px; float: left; font-family: "Open Sans", Arial, sans-serif;'><p style="margin-right: 0px; margin-bottom: 15px; margin-left: 0px; padding: 0px; text-align: justify;"><strong style="margin: 0px; padding: 0px;">Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><div><br></div></div><div style='margin: 0px 28.7969px 0px 14.3906px; padding: 0px; width: 436.797px; float: right; font-family: "Open Sans", Arial, sans-serif;'></div>
DATA;
$dom = new DOMDocument();
$dom->loadHTML($data, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
foreach($dom->getElementsByTagName('*') as $element ){
if ($element->hasAttribute('style')) {
$style = $element->getAttribute('style');
$replacement = preg_replace("/.*?\b(font-[^;] ;?).*|.*/", "$1", $style);
if (trim($replacement) !== "") {
$element->setAttribute('style', $replacement);
} else {
$element->removeAttribute('style');
}
}
}
echo $dom->saveHTML();
輸出
<div style='font-family: "Open Sans", Arial, sans-serif;'><p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><div><br></div><div style='font-family: "Open Sans", Arial, sans-serif;'></div></div>
uj5u.com熱心網友回復:
我認為你必須用兩個替換來做到這一點。第一次替換將只保留那些樣式包含字體系列規范的 HTML 標簽中的字體系列樣式:
使用以下正則運算式:
style=(['"])(?:[^\1>]*)(font-family:[^;] ;)(?:[^\1>]*)\1
帶替換:
style=$1$2$1
請參閱正則運算式演示
style=匹配“風格=”['"])匹配捕獲組 1 中的單引號或雙引號(?:[^\1>]*)匹配 0 個或多個不是 '>' 或捕獲組 1 的單引號或雙引號的字符(這確保我們不會掃描到當前 HTML 標簽的末尾)(font-family:[^;] ;匹配 font-family 宣告(?:[^\1>]*)匹配樣式宣告的其余部分(除開引號外的所有字符,確保我們不會掃描當前的 HTML 標簽)\1匹配開頭引號(捕獲組 1 中的任何內容,即單引號或雙引號)
下一個正則運算式將完全洗掉那些不包含字體系列規范的 HTML 標簽的樣式規范:
使用正則運算式:
\sstyle=(['"])(?![^\1>]*font-family:)(?:[^\1>]*)\1
并替換空字串,''
請參閱正則運算式演示
\sstyle=匹配空格后跟'style-'。(['"])匹配捕獲組 1 中的單引號或雙引號。(?![^\1>]*font-family:)一個否定的前瞻斷言,接下來的內容不是:0 個或多個與開頭參考不匹配的字符(什么是捕獲組 1)或 '>' 后跟 'font-family:。換句話說,此樣式規范不包含“font-family:”。(?:[^\1>]*)匹配 0 個或多個與起始引號(什么是捕獲組 1)或 '>' 不匹配的字符。\1匹配開頭的引號字符(捕獲組 1 中的內容。
PHP代碼
<?php
$html = <<<EOF
<div style='margin: 0px 14.3906px 0px 28.7969px; padding: 0px; width: 436.797px; float: left; font-family: "Open Sans", Arial, sans-serif;'><p style="margin-right: 0px; margin-bottom: 15px; margin-left: 0px; padding: 0px; text-align: justify;"><strong style="margin: 0px; padding: 0px;">Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><div><br></div></div><div style='margin: 0px 28.7969px 0px 14.3906px; padding: 0px; width: 436.797px; float: right; font-family: "Open Sans", Arial, sans-serif;'></div>
EOF;
$html = preg_replace('/style=([\'"])(?:[^\1>]*)(font-family:[^;] ;)(?:[^\1>]*)\1/', 'style=$1$2$1', $html);
$html = preg_replace('/\sstyle=([\'"])(?![^\1>]*font-family:)(?:[^\1>]*)\1/', '', $html);
echo $html;
印刷:
<div style='font-family: "Open Sans", Arial, sans-serif;'><p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><div><br></div></div><div style='font-family: "Open Sans", Arial, sans-serif;'></div>
見 PHP 演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/365245.html
