php strip_tags() function not working properly

Discussion in 'PHP' started by PinoyIto, May 31, 2009.

  1. #1
    The php strip_tags function work well for plain html codes but have problem dealing with css codes and javascripts like the following

    
    <style type="text/css">
    	.dap_category {
    		margin-bottom:3px; 
    		font-size:11px; 
    		font-weight:bold; 
    		color:#424242;
    		min-height:15px;
    	}
    	.dap_image_box {
    		min-height:110px;
    	}
    	.dap_no_image {
    		border:solid thin black; 
    		width:80px; 
    		height:60px; 
    		padding-top:20px; 
    		margin:0 auto
    	}
    	.dap_image {
    		border:solid thin black; 
    	}
    	.dap_item {
    		margin-top:5px; 
    		margin-bottom:2px;
    	}
    	a.dap_item_link {
    		color:black; 
    		font-size:11px; 
    		font-weight:bold
    	}
    	.dap_item_price {
    		font-size:11px; 
    		font-weight:bold; 
    		color:#B40404;
    	}
    </style>
    
    
    Code (markup):
    strip_tags remove only <style type="text/css"> and </style> but didn't remove the entire css code...

    Any way to solve this problem?

    thanks
     
    PinoyIto, May 31, 2009 IP
  2. hassanahmad2

    hassanahmad2 Active Member

    Messages:
    243
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #2
    try regular expressions then:

    
    $code = '<style type="text/css">
    	.dap_category {
    		margin-bottom:3px; 
    		font-size:11px; 
    		font-weight:bold; 
    		color:#424242;
    		min-height:15px;
    	}
    	.dap_image_box {
    		min-height:110px;
    	}
    	.dap_no_image {
    		border:solid thin black; 
    		width:80px; 
    		height:60px; 
    		padding-top:20px; 
    		margin:0 auto
    	}
    	.dap_image {
    		border:solid thin black; 
    	}
    	.dap_item {
    		margin-top:5px; 
    		margin-bottom:2px;
    	}
    	a.dap_item_link {
    		color:black; 
    		font-size:11px; 
    		font-weight:bold
    	}
    	.dap_item_price {
    		font-size:11px; 
    		font-weight:bold; 
    		color:#B40404;
    	}
    </style>';
    
    
    $code = eregi_replace('<style type="text/css">(.*)</style>', '', $code);
    echo $code;
    
    
    Code (markup):
     
    hassanahmad2, May 31, 2009 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    This should remove <style/> or <script/> elements.

    $str = preg_replace('#<(script|style)[^>]*>.+</\1>#Usi', '', $str);
    PHP:
     
    joebert, Jun 1, 2009 IP
  4. Ralle

    Ralle Active Member

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #4
    I dunno what Us means, if it means that it should be non-greedy. Otherwise I suggest this:
    $str = preg_replace('#<(script|style)[^>]*>.+?</\1>#i', '', $str);
    PHP:
     
    Ralle, Jun 1, 2009 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    You should probably read about the pattern modifiers if you're going to use the preg_* functions.
     
    joebert, Jun 2, 2009 IP