Thursday, April 28, 2011

Get Hex colors from string.

It will parse the string and collect all the unique hex colors.

<?php

/**  
 * get_hex_colors ()
 * Collect all unique hex colors from string.
 *
 * @author   Junaid Atari <mj.atari@gmail.com>
 * @version  1.0
 * @param    string   $str    String to check.
 * @return   array    List of unique hex colors
 */

function get_hex_colors ($str)
{
    
//** Pattern copyright 2011 Junaid Atari
    
    //** Hex colors range (A to F , 0 to 9)
    //** Supports 4 chars color code (with hash sign) #000
    //** Supports 7 chars color code (with hash sign) #000000
    //** Case insensitive
    
preg_match_all ("/#[a-f0-9]{6}|#[a-f0-9]{3}/i"$str,
                        
$resultsPREG_PATTERN_ORDER);
    
    
//** Remove duplicate hex entries.
    
return array_unique ($results[0]);
}

/*
 +========================================
 | EXAMPLE
 +========================================
*/
$str='
.SearchFormat
{    float:left;
    width:160px; height:17px;
    border:0px;
    border-left:1px dotted #ccc; 
    font-size:12px;
    color:#333;
    margin-top:4px;
    margin-left:30px;
    background-color:#fff;
}

.MainLinks
{    width:890px;
    float:left;
    top:133px;
    left:0px;
    text-align:right;
    color:#CCCCCC;
}
'
;
print_r get_hex_colors ($str) );

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

/*
Array
(
    [0] => #ccc
    [1] => #333
    [2] => #fff
    [3] => #CCCCCC
)
*/ 

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>