0

I ve downloaded css file in my localhost using html dom parser and ajax post..

<?php
        require'simple_html_dom.php';
        $srcCode = $_POST['doc'];       
        $url=$_POST['Url'];             
        $url_path=$url; 
        $html = file_get_html('template/'.$name.'.html');
        if(!file_exists('template/css'))    
            {mkdir('template/css',0777, true);}     
        foreach($html->find('link') as $link)    
        {
                $linkpath=$link->href;
                $links = explode("/", $linkpath);   
                $linkName = end($links);
                file_put_contents('/template/css/'.$linkName,file_get_contents($url_path.$linkpath));   
        }
?>

Now i ve to read those css file using php..or say i ve to get(dwnload) all the images src of those css..

background: url(../images/interface.png) no-repeat -1px -680px;
border: 0 none;
color: #534a39;
font-size: 16px;
height: 52px;
line-height: 42px;
margin: 24px auto 25px;
width: 281px;
text-indent: 40px;

how to parse css element?? how to get css content?? please do help me...

Bijay Rai
  • 961
  • 1
  • 12
  • 32

2 Answers2

1

this code is work

function parse($file){
    $css = file_get_contents($file);
    preg_match_all( '/(?ims)([a-z0-9\s\.\:#_\-@,]+)\{([^\}]*)\}/', $css, $arr);
    $result = array();
    foreach ($arr[0] as $i => $x){
        $selector = trim($arr[1][$i]);
        $rules = explode(';', trim($arr[2][$i]));
        $rules_arr = array();
        foreach ($rules as $strRule){
            if (!empty($strRule)){
                $rule = explode(":", $strRule);
                $rules_arr[trim($rule[0])] = trim($rule[1]);
            }
        }

        $selectors = explode(',', trim($selector));
        foreach ($selectors as $strSel){
            $result[$strSel] = $rules_arr;
        }
    }
    return $result;
}
$css = parse('filePath');

and use this

$css['#selector']['color'];
henry4343
  • 3,871
  • 5
  • 22
  • 30
0

Hi, I write simple code

 $yourRgx = '/images (\w+)/gi';
 $contentFile = file_get_contents('file.css');

 preg_match_all($yourRgx, $contentFile, $result);
 print_r($result);

logic like this :

1- Create your regex or text ("images/") or /images (\w+)/gi

2- Open the file with @file_get_contents

3- Use php preg_match_all() function and search all text

mehmetakifalp
  • 445
  • 5
  • 16