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
)
*/ 

0 comments:

Post a Comment