Hướng dẫn php remove style attribute from html - php xóa thuộc tính style khỏi html

Tôi sử dụng cái này:

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;
}

Tôi sử dụng chức năng này để lấy những thứ bên trong các điểm đánh dấu

bạn có thể sử dụng preg_rpace trên đó

Đó là khái niệm mà tôi cho rằng những gì bạn đang hỏi, chơi xung quanh nó, vấn đề của bạn cũng có thể bị phá vỡ đường dây trước khi bạn phân tích qua bất kỳ mã nguồn nào, khác với \ n

function get_insideMarkers($left, $right, $raw) {
$leftMarker = preg_quote($left, '/');
$rightMarker = preg_quote($right, '/');
$findBulk = preg_match_all('/'.$leftMarker.'(.*?)'.$rightMarker.'/', $raw, $array);
$resultArray = $array[1];
return $resultArray;
}

function stripnl($text) {
$text = str_replace("\r\n", "", $text);
$text = str_replace("\r", "", $text);
$text = str_replace("\n", "", $text);
return trim($text);
}

$raw = stripnl(file_get_contents('url'));
$array = get_insideMarkers('<span>', '</span>', $raw);
print_r($array); //returns all span tags

Xóa thuộc tính kiểu khỏi chuỗi với PHP

  • Tác giả: Salman
  • Ngày 01 tháng 6 năm 2018

Đôi khi bạn cần loại bỏ thuộc tính kiểu khỏi phần tử HTML. Chức năng dưới đây được thiết kế để thực hiện nhiệm vụ này.

Hàm số

function stripStyle($string) {
    
    return preg_replace('/(<[^>]+) style=".*?"/i', '$1', $string);     
}

Gọi chức năng

$string = '< div style="font-size:10px;color:red">Hello World!< /div>';

echo stripStyle($string);

Đầu ra

< div>Hello World!< /div>

Lớp bên dưới sẽ xóa thuộc tính kiểu khỏi nội dung HTML bằng Php Dom.

class RemoveStyle {
	

 public function formatStyle($html, $strip_tags = false) {
      if($html == "") return "";
      //$html = nl2br($html);
      $dom = $this->getDOMDocument($html);
      $xpath = new \DOMXPath($dom);
      $nodes = $xpath->query('//*');

      foreach ($nodes as $key => $node) {
        if($node->hasAttribute('style')) {
           $node->removeAttribute('style');
        }
      }

      $dom->saveHTML();
      $dombody = $dom->getElementsByTagName("body"); 
      $innerHTML = ""; 
      foreach ($dombody as $domnode) { 
        $innerHTML .= $this->getInnerHTML($domnode); 
      }
       if($strip_tags == true) {
        $innerHTML = strip_tags($innerHTML);
       }
      return $innerHTML; 
  }



  public function getDOMDocument($html) {
   $dom = new \DOMDocument;
   libxml_use_internal_errors(true);
   //$dom->loadHTML($html);
   $dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
   libxml_use_internal_errors(false);  
   return $dom;
  }


    /**
   * Returns inner html
   *
   * @return string
   *   Returns string.
   */
  public  function getInnerHTML($element) { 
    $innerHTML = ""; 
    $children  = $element->childNodes;
    foreach ($children as $child) { 
      $innerHTML .= $element->ownerDocument->saveHTML($child);
    }
    return $innerHTML; 
  }

}

$Rs = new RemoveStyle();
$someHtml = "<h2 style='display:none;'></h2>";
$output = $Rs->formatStyle($someHtml);

Lớp sẽ xóa thuộc tính kiểu khỏi nội dung HTML bằng Php Dom.& nbsp;
 

Làm thế nào để xóa một thẻ kiểu trong HTML trong PHP?

Chức năng Strip_tags () là một hàm sẵn có trong PHP loại bỏ các chuỗi HTML, XML và PHP.strip_tags() function is an inbuilt function in PHP that removes the strings form HTML, XML and PHP tags.

Làm thế nào để loại bỏ thuộc tính kiểu bằng PHP?

Phong cách regex thực dụng (]+) = ".*?"sẽ giải quyết vấn đề này trong tất cả các trường hợp hợp lý.Một phần của trận đấu không phải là nhóm bị bắt đầu tiên nên được loại bỏ, như thế này: $ output = preg_replace ('/(]+) style = ".pragmatic regex (<[^>]+) style=". *?" will solve this problem in all reasonable cases. The part of the match that is not the first captured group should be removed, like this: $output = preg_replace('/(<[^>]+) style=".

Làm thế nào để loại bỏ kiểu trong thẻ HTML?

Sử dụng phương thức removeAtribution () để loại bỏ tất cả các kiểu khỏi một phần tử, ví dụ:hộp.RemoveAttribution ('Style').Phương thức RemoveAtribution sẽ loại bỏ thuộc tính kiểu khỏi phần tử.Đây là HTML cho các ví dụ trong bài viết này., e.g. box. removeAttribute('style') . The removeAttribute method will remove the style attribute from the element. Here is the HTML for the examples in this article.