2011-04-01 130 views
35

我不使用正则表达式不太好,但与PHP,我想删除HTML标签style属性在的到来,从TinyMCE的回一个字符串。删除样式属性

因此改变<p style="...">Text</p>只是香草<p>Test</p>

我怎么会用类似的preg_replace()功能实现这一目标?

+4

你**不**想用正则表达式这种。 – 2011-04-01 18:25:44

+3

如果不推荐正则表达式用于此任务,那么*是*是什么? – 2011-04-01 18:40:56

+1

使用php的DOM方法。 – Maerlyn 2011-04-01 18:41:55

回答

106

务实的正则表达式(<[^>]+) style=".*?"将在所有合理的情况下解决这个问题。本场比赛的一部分,是不是第一个捕获组应该被删除,就像这样:

$output = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $input); 

匹配一个<后面跟着一个或多个“不>”直到我们来到space和的style="..."部分。 /i甚至可以使用STYLE="..."。将此匹配替换为$1,这是捕获的组。如果标签不包含style="...",它将保持原样。

+1

+1 ---刚刚使用它来摆脱Google协作平台网页中发生的样式垃圾信息,当用户在智能编辑器中进行粘贴而不使用在浏览器中“粘贴为纯文本”。我使用了Notepad ++和replace命令。对于Notepad ++,格式为“\ 1”,而不是“$ 1”。 – Fuhrmanator 2013-03-12 16:55:50

+0

在样式转义时不起作用。它应该被重构,以使其工作或$输入应该用stripcslashes($输入) – bicycle 2013-03-24 15:57:09

+8

这将无法与style ='...' – q0rban 2013-04-30 19:13:11

-8

你可以处理它的客户端,最简单的是用jQuery。喜欢的东西:

$("#tinyMce p").removeAttr("style"); 
+2

这是不安全的,因为用户仍然可以篡改提交给服务器的数据。 – ThiefMaster 2011-04-01 18:34:53

+0

原来的海报问如何用PHP做到这一点,而不是JavaScript。 Quote:“...但与PHP我想要删除样式属性...”#: – 2015-08-10 05:13:08

4

在这里你去:

<?php 

$html = '<p style="border: 1px solid red;">Test</p>'; 
echo preg_replace('/<p style="(.+?)">(.+?)<\/p>/i', "<p>$2</p>", $html); 

?> 

顺便说一句,因为被别人指出的那样,正则表达式,不建议这一点。

+2

'''''''''''''' - BOOM。 '

- BOOM。 – ThiefMaster 2011-04-01 18:35:31

+1

这显然是在原始问题报告的例子上制作的。您仍然可以提高使用像这样的正则表达式:<?P(*)的风格= “(+)。?”(*)。?> 的preg_replace('/(+?)<\/p>/i',“

$ 4

”,$ links);并没有获得BOOM :) – 2011-04-01 18:44:21

+0

@LorenzoMarcon谢谢。我刚刚添加了基于您评论的正则表达式的反向匹配。 – RafaSashi 2014-05-27 23:21:03

35

像这样的东西应该工作(未经测试的代码警告):

<?php 

$html = '<p style="asd">qwe</p><br /><p class="qwe">qweqweqwe</p>'; 

$domd = new DOMDocument(); 
libxml_use_internal_errors(true); 
$domd->loadHTML($html); 
libxml_use_internal_errors(false); 

$domx = new DOMXPath($domd); 
$items = $domx->query("//p[@style]"); 

foreach($items as $item) { 
    $item->removeAttribute("style"); 
} 

echo $domd->saveHTML(); 
+10

+1使用正确的,无正则表达式的解决方案 – ThiefMaster 2011-04-01 18:55:42

+0

确认我已经测试了这个代码,它确实有效。 +1 – JaseC 2013-07-29 23:34:21

+1

除了上面我的评论......它确实有效,但DOMDocument在html实体中遇到了问题。它会将它们转换为utf8字符。所以&交易;成为TM。最后,我使用了simplehtmldom,并将我的功能作为答案... – JaseC 2013-09-24 00:41:04

2

我用这个:

function strip_word_html($text, $allowed_tags = '<a><ul><li><b><i><sup><sub><em><strong><u><br><br/><br /><p><h2><h3><h4><h5><h6>') 
{ 
    mb_regex_encoding('UTF-8'); 
    //replace MS special characters first 
    $search = array('/&lsquo;/u', '/&rsquo;/u', '/&ldquo;/u', '/&rdquo;/u', '/&mdash;/u'); 
    $replace = array('\'', '\'', '"', '"', '-'); 
    $text = preg_replace($search, $replace, $text); 
    //make sure _all_ html entities are converted to the plain ascii equivalents - it appears 
    //in some MS headers, some html entities are encoded and some aren't 
    //$text = html_entity_decode($text, ENT_QUOTES, 'UTF-8'); 
    //try to strip out any C style comments first, since these, embedded in html comments, seem to 
    //prevent strip_tags from removing html comments (MS Word introduced combination) 
    if(mb_stripos($text, '/*') !== FALSE){ 
     $text = mb_eregi_replace('#/\*.*?\*/#s', '', $text, 'm'); 
    } 
    //introduce a space into any arithmetic expressions that could be caught by strip_tags so that they won't be 
    //'<1' becomes '< 1'(note: somewhat application specific) 
    $text = preg_replace(array('/<([0-9]+)/'), array('< $1'), $text); 
    $text = strip_tags($text, $allowed_tags); 
    //eliminate extraneous whitespace from start and end of line, or anywhere there are two or more spaces, convert it to one 
    $text = preg_replace(array('/^\s\s+/', '/\s\s+$/', '/\s\s+/u'), array('', '', ' '), $text); 
    //strip out inline css and simplify style tags 
    $search = array('#<(strong|b)[^>]*>(.*?)</(strong|b)>#isu', '#<(em|i)[^>]*>(.*?)</(em|i)>#isu', '#<u[^>]*>(.*?)</u>#isu'); 
    $replace = array('<b>$2</b>', '<i>$2</i>', '<u>$1</u>'); 
    $text = preg_replace($search, $replace, $text); 
    //on some of the ?newer MS Word exports, where you get conditionals of the form 'if gte mso 9', etc., it appears 
    //that whatever is in one of the html comments prevents strip_tags from eradicating the html comment that contains 
    //some MS Style Definitions - this last bit gets rid of any leftover comments */ 
    $num_matches = preg_match_all("/\<!--/u", $text, $matches); 
    if($num_matches){ 
     $text = preg_replace('/\<!--(.)*--\>/isu', '', $text); 
    } 
    $text = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $text); 
return $text; 
} 
+4

看起来......详细。 – 2013-04-20 22:57:03

19

我对@Mayerln的功能评价。它确实工作,但DOMDocument真的与编码。这里是我的simplehtmldom版本

function stripAttributes($html,$attribs) { 
    $dom = new simple_html_dom(); 
    $dom->load($html); 
    foreach($attribs as $attrib) 
     foreach($dom->find("*[$attrib]") as $e) 
      $e->$attrib = null; 
    $dom->load($dom->save()); 
    return $dom->save(); 
} 
+1

可以确认,这个作品非常好!(谢谢你节省我的一天) – Mathlight 2015-06-11 17:38:57

+2

这可以被增强以删除任何和所有的属性? – u01jmg3 2016-11-11 13:07:41

1

除了洛伦佐马尔康的回答是:

使用preg_replace选择不同的样式属性的一切:

$html = preg_replace('/(<p.+?)style=".+?"(>.+?)/i', "$1$2", $html);