#1
  1. Banned

    Join Date
    Jul 2004
    Location
    The Mews At Windsor Heights
    Posts
    5,326
    Rep Power
    0

    Question Inline CSS attributes?


    I'm trying to get the width + height from an inline "style" attribute. I've almost got it but I can't get the height without overwriting the width with border-width

    How do I say "Match width or height anywhere (including start of string) except when they are preceded by -" ?
    Code:
    //width pattern
    #[^\-]?width:\s*(\d+)px#i
    
    // height pattern
    #[^\-]?height:\s*(\d+)px#i
    
    // examples
    border-width:0px;height:178px;width:117px;
    w: 0
    h: 178
    
    width:117px;height:178px;border-width:0px;
    w: 117
    h: 178
    
    height:178px;width:117px;border-width:0px;
    w: 117
    h: 178
    
    border-width:0px;
    w: 0
    h:
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,885
    Rep Power
    6353
    You can split up this regexp if you like, I wrote it "all in one":
    PHP Code:
    $strings = array("border-width:0px;height:178px;width:117px;""width:117px;height:178px;border-width:0px;""height:178px;width:117px;border-width:0px;""border-width:0px;");
    foreach ( 
    $strings as $string ) {
        if (
    preg_match_all('/(((?<!-)width)|((?<!-)height)):\s*([\d\.]+)/i'$string$foo) ) {
            foreach ( 
    $foo[4] as $key => $val ) {
                echo 
    "In the string: <b>{$string}</b> we found {$foo[1][$key]}{$val}<br />";
            }
            echo 
    "<P />";
        } else {
            echo 
    "No matches found for <b>{$string}</b><P />";
        }
    }
    die(); 
    The main point is that you need a negative lookbehind:
    Code:
    (?<!-)width
    matches "width without a hyphen in front of it"

    -Dan

    Comments on this post

    • b3n agrees
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. Banned

    Join Date
    Jul 2004
    Location
    The Mews At Windsor Heights
    Posts
    5,326
    Rep Power
    0
    Thanks Dan - I'll have to try that.

    I actually cheated and used explode() It works but it's more code than seems necessary. Speed is creeping up again... now I'm nearing completion of this script I will soon compare it to DomDocument again.

    Here is my klunky explode():
    PHP Code:
    <?php
    preg_match
    ('/ width=[\'"]?([0-9]+)[^\'"]?/i'$tag$wmatches);
    preg_match('/ height=[\'"]?([0-9]+)[^\'"]?/i'$tag$hmatches);
        
    $w = (isset($wmatches[1])) ? $wmatches[1] : '' 
    $h = (isset($hmatches[1])) ? $hmatches[1] : '' ;

    if (empty(
    $w) || empty($h))
    {
        
    preg_match("# style=['\"](.*)['\"]#i"$tag$style);
        
        if (isset(
    $style[1]) && ! empty($style[1]))
        {
            
    $style_array explode(';'$style[1]);
            
            
    //echo '<pre>style: '.print_r($style_array,1).'</pre>';
            
            
    foreach($style_array as $attr)
            {
                if (empty(
    $w))
                {
                    
    preg_match("#^width:\s*(\d+)px#i"$attr$wmatches);
                    
                    
    // if ( ! empty($wmatches)) echo '<pre>wmatches: '.print_r($wmatches,1).'</pre>';
                    
                    
    if (isset($wmatches[1]) && ! empty($wmatches[1])) $w $wmatches[1];
                }
                
                if (empty(
    $h))
                {
                    
    preg_match("#^height:\s*(\d+)px#i"$attr$hmatches);
                    
                    
    // if ( ! empty($hmatches)) echo '<pre>hmatches: '.print_r($hmatches,1).'</pre>'; 
                    
                    
    if (isset($hmatches[1]) && ! empty($hmatches[1])) $h $hmatches[1]; 
                }
                
                if ( ! empty(
    $w) && ! empty($h)) break;  
            }
        }
    }

    // errors suppressed because I cannot be bothered to fix filenames like "Alice 6.jpg" just for this  =/
    if (empty($w) && empty($h)) list($w$h$type$attr) =  @ getimagesize($src);
    ?>

IMN logo majestic logo threadwatch logo seochat tools logo