Regex Pattern Matching

Discussion in 'PHP' started by JosS, Jun 4, 2007.

  1. #1
    Say I have a stylesheet, and I want to grab certain values out. For example, I need the the script to parse through this:

    <style type="text/css">
    
    
    table, tr, td { background-color:transparent; border:none; border-width:0;}
    body {
    	background-color:fd00c2;
            background-image:url('bg.gif');
    	background-attachment:fixed;
    	background-position:bottom left;
    	background-repeat:no-repeat;
    	 }
    
    table table { border: 0px }
    table table table table{border:0px}
    table table table {
    	border-style:solid;
    	border-width:8px;
    border-color:000;
    	background-color:fd00c2;
    
    
    PHP:
    and I needed to grab these parts out

    border-color:000;
    PHP:
    body {
    	background-color:fd00c2;
            background-image:url('bg.gif');
    	background-attachment:fixed;
    	background-position:bottom left;
    	background-repeat:no-repeat;
    	 }
    
    PHP:
    . And another thing is, the code could be jumbled up aswell, as in, body { } could be at the bottom of the file.

    How could you match up, and grab out these sections?

    This is what I've got so far:

    <?php
    $code = stripslashes($_GET['code']);
    preg_match("~(.*?background-color:.*?)~", $code, $bgcolor);
    echo $bgcolor[0];
    ?>
    
    
    
    PHP:
    If I knew how to understand what all this .*?~ etc is in the pattern matching, I'm thinking I could

    find the body { section then echo everything inside it, until it reaches the next }, and for the next part I could look for the table table table { section then echo the border-color:000000 until it reaches the next } and so on.

    Then theres the problem with if all the code is on one line!
     
    JosS, Jun 4, 2007 IP