Question

My Code:

    $css="
    .class1{
        padding:10px 15px 0 auto;
        padding:10px 150px 0 20px;
        padding:13px 30px 10px 50px;
        padding:24px 1px 0 -20px;
    }
    ";

Function below extracts content between [padding:] and [;]

    function extract_unit($css, $start, $end){
    $pos = stripos($css, $start);
    $str = substr($css, $pos);
    $str_two = substr($str, strlen($start));
    $second_pos = stripos($str_two, $end);
    $str_three = substr($str_two, 0, $second_pos);
    $unit = trim($str_three); // remove whitespaces
    echo $unit;
    return $unit;
    }

    echo extract_unit($css , 'padding:',';');

Output: 10px 15px 0 auto

How can i extract all paddings in an array using this function.. So the result i need to be like this :

    array(
    "10px 15px 0 auto",
    "10px 150px 0 20p",
    "13px 30px 10px 50px",
    "24px 1px 0 -20px"
    );
Was it helpful?

Solution

You can use a regex for this

preg_match_all("/padding:(.*);/siU",$css,$output);

print_r($output[1]);

demo

OTHER TIPS

explode can do some trick:

// first get content inside class{} and remove "padding:"
$parts = explode( "{", $css );
$parts = str_replace( "padding:" , "", explode( "}", $parts['1'] ) );

// now break it with ';'
$padding = explode( ";", $parts['0'] );

Test Here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top