Results 1 to 9 of 9

Thread: non-inclusive preg_match

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default non-inclusive preg_match

    What would be the syntax if I wanted to find the content between two terms, but I don't want the two terms included in the match using preg_match() to get my value? For example:

    PHP Code:
    .css {
    color:#33FF44;
    fontbold 10pt verdanageneva;

    and I want to capture the #33FF44 alone. The #33FF44 could be in the format of 33FF44 #33FF44 or 333333 or scrollbar or blue, etc.

    The first part to look for would be .css {
    color:
    the second would be ;

    thanks
    Last edited by james438; 07-24-2008 at 10:57 PM. Reason: just bolded some text.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    This should work for you james438:
    PHP Code:
    <?php
    $css 
    = <<< CSS
    .css {
    color:#33FF44;
    font: bold 10pt verdana, geneva;
    }  
    CSS;

    $cssPos strpos($css,'#');
    echo 
    substr($css,$cssPos,7);
    ?>
    Jeremy | jfein.net

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    It doesn't really help. It doesn't explain the syntax of preg_match() and the script you gave would only work if the color code were exactly 6 characters long and didn't have additional code on the same line. In other words it would only work under very strict conditions, which is why I wanted to use a regular expression. I do not know what format I or someone else will want to use when creating a color code, especially since I will not be using a dropdown menu of colors to choose from or have a selection of colors for the person or myself to choose from.

    I figured the problem out though. preg_match() operates a little differently than preg_replace, but is really quite similar in syntax. In this case what is neded is the use of subpatterns to capture the desired data. Take a look at the following example:

    Code:
    <?php
    $css = "                                        .css { 
    color:#scrollbar; 
    font: bold 10pt verdana, geneva; 
    } ";
    
    preg_match('/\.css \{ 
    color:(.*?);/', $css, $match);
    print_r($match);
    ?>
    Here $css contains the data. We then use preg_match to locate the desired pattern. preg_match() operates in the following format preg_match('/pattern/','data',$array); where the first matched pattern and its subpatterns found will be inserted into $array or in the above example $match.

    The command print_r($array); is merely a way to traverse an array. If you wanted to match all of the patterns and their respexctive subpatterns you would use preg_match_all() which is in the same format, but the matches would be inserted into a multidimensional array, but that is just a little extra info.

    PS: I added a bit of extra info for the accidental reader who might be a little confused about the code above.
    Last edited by james438; 07-25-2008 at 03:09 PM.

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Sorry, my mistake. I thought you wanted to do it without preg_match.
    And your wrong in the 7 digit thing, there's no hex longer then 6 digits. And explain to me what this does:
    Code:
    color:#scrollbar;
    Jeremy | jfein.net

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    don't think I said I was using hex I could be using RGB format or just type out the word "red" or "scrollbar". As far as "#scrollbar" goes, it is an intentioanl typo. I added it in case the user used the color "scrollbar" and accidentally prefaced it with the octothorpe, because they thought it was necessary.

    I use the color scrollbar occasionally on my website. Here is an example:
    Code:
    <style type="text/css">
    .css { 
    color:scrollbar; 
    font: bold 20pt verdana, geneva;
    }
    </style>
    <span class="css">yo</style>
    Other formats include:
    color: #f00;
    color: #ff0000;
    color: rgb(255,0,0);
    color: rgb(100%, 0%, 0%);

    I was reading from the CSS site about the different color code formats.

    PS: yep, I could have worded my first post a little better now that I have reread it.
    Last edited by james438; 07-25-2008 at 03:07 PM. Reason: typos and added PS. more typos and aded quotes.

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    What does color: scrollbar do?
    Jeremy | jfein.net

  7. #7
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    "scrollbar" attempts to match the scrollbar color for the OS, possibly the browser but i'm not sure. it seems to match the color of windows panel colors such as the taskbar and scrollbar of IE on w2ksp4

    So a few questions.

    1. If you have something like the code in your first post, do you want it to get each of the specified styles like font AND color in that example, or are you only trying to get color?
    Ex:
    Code:
    .css { 
    color:#33FF44; 
    font: bold 10pt verdana, geneva; 
    }
    Only pull the color: part, or the color: and the font: part?

    2. How many style brackets are there going to be? Like only a css{} or will there be css{}css2{} etc?
    Will it always be:
    Code:
    .css { 
    color:#33FF44; 
    font: bold 10pt verdana, geneva; 
    }
    or can it sometimes be something like:
    Code:
    .css { 
    color:#33FF44; 
    font: bold 10pt verdana, geneva; 
    } 
    .css2 { 
    color:#FF0000; 
    font: 12pt arial, tahoma; 
    }
    3. If there are multiple style brackets, are you trying to get what you want from each one, or do you want to specify which one(s) you pull the selected style from?
    Ex. if you chose this in question 2:
    Code:
    .css { 
    color:#33FF44; 
    font: bold 10pt verdana, geneva; 
    } 
    .css2 { 
    color:#FF0000; 
    font: 12pt arial, tahoma; 
    }
    Do you want to be able to pull what you want (which you chose in question1) from all sets there (css and css2) or do you want to be able to pick which one(s) you want to pull from?

  8. #8
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    scrollbar is a color, like red, or blue Try it out and you will see that what color it is. sort of an off white. http://www.tntluoma.com/sidebars/triplecolors/ lists a good deal of the named colors.

    I just want to pull the color and nothing more. If I wanted more like in example 3 I would use an or symbol "|" but lets not make things more complicated than they need to be, because multidimensional arrays give me the shivers!

  9. #9
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    Code:
    <?php
    $css = ".css {
    color:#33FF44;
    font: bold 10pt verdana, geneva;
    }";
    
    preg_match('/color:(.*?);/', $css, $match);
    print_r($match);
    $color = $match[1];
    echo "<br><br>$color";
    ?>
    Returns:
    Array ( [0] => color:#33FF44; [1] => #33FF44 )

    #33FF44
    Therefore, "$color" is now the color you were trying to get.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •