Thread: split the class
View Single Post
Old 12-22-2010, 03:11 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
In the future please wrap code in [code][/code] or [php][/php] tags to preserve the formatting.

So you are just wanting to print out all used CSS classes within a file?
A simple preg is the easiest way to do this, and will happen in the do_check function. I'd read this as a string since you can match once with the preg instead of each line, plus most of do_check does nothing. I'd strongly recommend the removal of your global variables as well and opt to pass in any parameters you need or use constants if its for read only.
PHP Code:
function do_check($path)
{ global 
$in_string$out_string ;

// echo "in fn do_check in is $in_string out is $out_string <br>";

if (filesize($path)>250000)
{
echo 
"<span style='background: blue'>FILE TOO LONG $path</span>";
return;
}

// echo $path.'<br>';
    
if (false !== ($sFile = @file_get_contents($path)))
    {
        
$sPattern '#class=("|\')([^{\1}]+)\1#miU';
        
$aMatches = array();
        if (
preg_match_all($sPattern$sFile$aMatches))
        {
            
$aClasses = array();
            foreach (
$aMatches[2] AS $class)
            {
                
$parts explode(" "$class);
                
$aClasses array_merge($parts$aClasses);
            }
        }
    }

$aClasses will contain all classes. Use array_unique to remove duplicates.
$aMatches itself will contain the full block (the class=...) and the quotations in use. Those will be offsets 0 and 1.
If you need this line by line then I wouldn't match it as that will be slow. You would need to use strpos with substr to essentially cut out sections of the string to assign the part to a variable. Not sure why you'd want to do that though.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote