1

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"
    );
Naveed
  • 41,517
  • 32
  • 98
  • 131
Montaser El-sawy
  • 800
  • 1
  • 8
  • 14

2 Answers2

3

You can use a regex for this

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

print_r($output[1]);

demo

Gordon
  • 312,688
  • 75
  • 539
  • 559
Altaf Hussain
  • 1,048
  • 2
  • 12
  • 25
3

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

Naveed
  • 41,517
  • 32
  • 98
  • 131
  • You can write last statement as `$padding = explode(";", trim(trim($parts['0']), ";"));` to avoid last empty array location in resultant array. – Naveed May 20 '12 at 09:13