PDA

View Full Version : non-inclusive preg_match



james438
07-23-2008, 06:03 PM
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:


.css {
color:#33FF44;
font: bold 10pt verdana, geneva;
}
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 :)

Nile
07-24-2008, 03:03 AM
This should work for you james438:


<?php
$css = <<< CSS
.css {
color:#33FF44;
font: bold 10pt verdana, geneva;
}
CSS;

$cssPos = strpos($css,'#');
echo substr($css,$cssPos,7);
?>

james438
07-24-2008, 10:55 PM
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:


<?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.

Nile
07-24-2008, 11:01 PM
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:


color:#scrollbar;

james438
07-25-2008, 12:04 AM
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:

<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.

Nile
07-25-2008, 01:24 AM
What does color: scrollbar do?

motormichael12
07-25-2008, 02:26 AM
"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:

.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:

.css {
color:#33FF44;
font: bold 10pt verdana, geneva;
}
or can it sometimes be something like:

.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:

.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?

james438
07-25-2008, 03:13 AM
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!

motormichael12
07-25-2008, 03:57 AM
<?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.