Thursday, April 28, 2011

Remove inline CSS from HTML

Here, Please checkout the code:

<?php
/** 
 * remove_inline_css ()
 * Remove the inline CSS styles for HTML tags.
 *
 * @author   Junaid Atari <mj.atari@gmail.com>
 * @version  1.0
 * @param    string   $subject    String to remove styles.
 * @return   string   Filtered HTML string.
 */

function remove_inline_css ($subject)
{
    
//** Return if invalid type given.
    
if (!is_string ($subject)

        || trim ($subject) == '')
            return 
'No text given.';
    
    
//** Create the anonymous function on Runtime.
    
$cr create_function ('$matches',

              'return str_replace ($matches[2], "", $matches[0]);');
    
    
//** Pattern copyright 2011 Junaid Atari
    //** Return with Regex, only find the style=".*" attribute

         of any tag
    //** and replace using callback.
    
return preg_replace_callback ('/(<[^>]+( style=".*").*>)/iU',
            
$cr,
            
$subject);
}

/*
 +========================================
 | EXAMPLE
 +========================================
*/

$subject '<a target="_blank" style="color:red;">This is my test</a>';
echo remove_inline_css ($subject); 


/*
 +========================================
 | OUTPUT
 +========================================
*/

//<a target="_blank">This is my test</a>

0 comments:

Post a Comment