2010-05-18 11 views
5

Fondamentalement, je veux ceci:Fonction PHP pour diviser un attribut CSS combiné/valeur à plusieurs attributs

h2 { 
    font: bold 36px/2em "Times New Roman" 
} 

à ceci:

h2 { 
    font-size: 36px; 
    font-weight: bold; 
    line-height: 2em; 
    font-family: "Times New Roman" 
} 

et autres variations, bien sûr. Est-ce que quelqu'un connaît une fonction qui fait cela, donc je n'ai pas à le coder moi-même? :)

+2

Je voudrais avoir une fonction qui fait l'inverse .. –

+1

@Alix Axel: Yup qui est meilleur choix et plus logique :) – Sarfraz

+0

Oui, je voudrais qu'il gère les deux sens, bien sûr :) – Sandman

Répondre

3

Sur la base de cette CSS Shorthand for the Font Element:

CSS Font Shorthand Style Guide

Je suis venu avec l'expression régulière suivante:

font:(?:\s+(inherit|normal|italic|oblique))?(?:\s+(inherit|normal|small-caps))?(?:\s+(inherit|normal|bold(?:er)?|lighter|[1-9]00))?(?:\s+(\d+(?:%|px|em|pt)?|(?:x(?:x)?-)?(?:small|large)r?)|medium|inherit)(?:\/(\d+(?:%|px|em|pt)?|normal|inherit))?(?:\s+(inherit|default|.+?));?$ 

Obtenu à partir de ces petites expressions régulières:

$font['style'] = '(?:\s+(inherit|normal|italic|oblique))?'; 
$font['variant'] = '(?:\s+(inherit|normal|small-caps))?'; 
$font['weight'] = '(?:\s+(inherit|normal|bold(?:er)?|lighter|[1-9]00))?'; 
$font['size'] = '(?:\s+(\d+(?:%|px|em|pt)?|(?:x(?:x)?-)?(?:small|large)r?)|medium|inherit)'; 
$font['height'] = '(?:\/(\d+(?:%|px|em|pt)?|normal|inherit))?'; 
$font['family'] = '(?:\s+(inherit|default|.+?))'; 

Utilisation:

$regex = 'font:' . implode('', $font) . ';?$';  
$matches = array(); 
$shorthand = 'font: bold 36px/2em Arial, Verdana, "Times New Roman";'; 

if (preg_match('~' . $regex . '~i', $shorthand, $matches) > 0) 
{ 
    echo '<pre>';  
    if (strlen($matches[1]) > 0) { // font-style is optional 
     print_r('font-style: ' . $matches[1] . ';' . "\n"); 
    } 

    if (strlen($matches[2]) > 0) { // font-variant is optional 
     print_r('font-variant: ' . $matches[2] . ';' . "\n"); 
    } 

    if (strlen($matches[3]) > 0) { // font-weight is optional 
     print_r('font-weight: ' . $matches[3] . ';' . "\n"); 
    } 

    print_r('font-size: ' . $matches[4] . ';' . "\n"); // required 

    if (strlen($matches[5]) > 0) { // line-height is optional 
     print_r('line-height: ' . $matches[5] . ';' . "\n"); 
    } 

    print_r('font-family: ' . $matches[6] . ';' . "\n"); // required 
    echo '</pre>'; 

    echo '<pre>'; 
    print_r($matches); 
    echo '</pre>'; 
} 

Sortie:

font-weight: bold; 
font-size: 36px; 
line-height: 2em; 
font-family: Arial, Verdana, "Times New Roman"; 

Array 
(
    [0] => font: bold 36px/2em Arial, Verdana, "Times New Roman"; 
    [1] => 
    [2] => 
    [3] => bold 
    [4] => 36px 
    [5] => 2em 
    [6] => Arial, Verdana, "Times New Roman" 
) 

Ceci est destiné à l'extraction pas de validation puisqu'il accepte des choses comme xx-plus petite (qui n'est pas valide).

Pour faire la version étendue, vous pouvez utiliser preg_match() ou preg_replace(), bien que l'utilisation de ce dernier serait plus difficile à "ignorer" les déclarations inutilisées.

+0

Merci! Je regarderai plus loin dans ceci! – Sandman

+0

@Sandman: De rien. :) –

0

Il n'y a pas une telle fonction en PHP. Vous pouvez créer une telle fonction - je commencerais par une carte qui contient [styleRuleName => [{sets of valid attributes}]] pour chaque sélecteur de raccourcis que vous souhaitez développer. Je pense que vous trouverez que c'est un peu un cauchemar à coder.

+1

De Bien sûr, il n'y a pas de fonction intégrée, je voulais dire une fonction tierce. Je pourrais évidemment le coder moi-même, mais je ne veux pas :) – Sandman

+0

Pas non plus au courant d'une telle fonction. La plupart des gens sont préoccupés par la combinaison des attributs, pas par leur expansion. – Finbarr