0

I have that in a variable :

.fontSize10{
font-size:xx-small;
}
.fontSize11{
font-size:x-small;
}
.fontSize12{
font-size:small;
}
.fontSize13{
font-size:medium;
}
.fontSize14{
font-size:large;
}
.fontSize16{
font-size:x-large;
}
.fontSize18{
font-size:xx-large;
}

In this variable, i can have a lots of other css instructions.

I need to retrieve, for each instructions that begin by .fontSize, his name (for example .fontSize14) and his size (large).

I need to keep name and size together (in an array for example $tab[0][0] = "fontSize14" and $tab[0][1] = "large")

Do you have an idea ? With regex maybe ? Thanks :)

2
  • How about .fontSize3 .otherClass? Is this going to be 1 time task? Commented Jan 30, 2013 at 9:52
  • the question not clear for me... Commented Jan 30, 2013 at 9:53

3 Answers 3

3

If it strictly keeps to this format, you could do it with a regular expression like this:

=\.(fontSize\d+)\s*\{\s*font-size:([^;]+);\s*\}=is

However, I would advise you to write a proper parser so you can deal with wrong or different formats. Meaning that you iterate through the string line by line and store in a variable the name of the current definition block. If you encounter a font-size: you can just add the found value to an array using the name of the current block. When you encounter a closing bracket, you just set the variable back to null. It will be much more robust than the regular expression, because it works for CSS definitions with more attributes just as well.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried it and it works. Have a look here. The page is in German, but you should get the drift.
With "preg_match_all("/=\.(fontSize\d+)\s*\{\s*font-size:([^;]+);\s*\}=is/ms", $css, $size, PREG_PATTERN_ORDER, 0);", my array size is empty...
1

Try to build your CSS as an array, then search through it, and when your done searching, implode the array (using 'implode()').

Example of the array structure:

$css = array('.fontSize10' => 'xx-small', '.fontSize11' => 'x-small');

If you really want to keep using $css as a string, then regex is the way to go.

Take a look at the following:

Comments

0

Maybe you do a loop with some explodes included:

$result = array();
$parts1 = explode('}', $css);
foreach($parts1 as $part){
    if(strpos($part, '.fontSize') === 0){
        $subparts = explode('{', $part);
        $name = $subparts[0];
        //continue here to parse out the correct css value, maybe split again at ; and again at :
        //you then can pick out the parts you need and store it in $value or whatever.
        $result[] = array('name' => $name, 'value' => $value);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.