Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6159583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:09:24+00:00 2026-05-23T21:09:24+00:00

Given a font CSS string such as this: font:italic bold 12px/30px Georgia, serif; or

  • 0

Given a font CSS string such as this:

font:italic bold 12px/30px Georgia, serif;

or

font:12px verdana;

I want to convert it to its long hand format i.e:

font-style: italic; font-weight: bold;

Here is my miserable attempt: http://pastebin.com/e3KdMvGT

But of course it doesn’t work for the second example since its expecting things to be in order, how can I improve it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:09:24+00:00Added an answer on May 23, 2026 at 9:09 pm

    Here is a function which should do the work. The problem lies in the font-style, font-variant and font-weight properties and the value “normal” as you can read in the css specs ([[ <'font-style'> || <'font-variant'> || <'font-weight'> ]? <'font-size'> [ / <'line-height'> ]? <'font-family'> ] | caption | icon | menu | message-box | small-caption | status-bar | inherit).

    $testStrings = array('12px/14px sans-serif',
                         '80% sans-serif',
                         'x-large/110% "New Century Schoolbook", serif',
                         'x-large/110% "New Century Schoolbook"',
                         'bold italic large Palatino, serif ',
                         'normal small-caps 120%/120% fantasy',
                         'italic bold 12px/30px Georgia, serif',
                         '12px verdana');
    
    foreach($testStrings as $font){
      echo "Test ($font)\n<pre>
    ";
      $details = extractFull($font);
      print_r($details);
      echo "
    </pre>";
    }
    
    
    function extractFull($fontString){
      // Split $fontString. The only area where quotes should be found is around font-families. Which are at the end.
      $parts = preg_split('`("|\')`', $fontString, 2, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
    
      $chunks = preg_split('` `', $parts[0], NULL, PREG_SPLIT_NO_EMPTY);
      if(isset($parts[1])){
        $chunks[] = $parts[1] . $parts[2];
      }
    
      $details = array();
      $next = -1;
    
      // Manage font-style / font-variant / font-weight properties
      $possibilities = array();
      $fontStyle = array('italic', 'oblique');
      $fontVariant = array('small-caps');
      $fontWeight = array('bold', 'bolder', 'lighter', '100', '200', '300', '400', '500', '600', '700', '800', '900');
    
      // First pass on chunks 0 to 2 to see what each can be
      for($i = 0; $i < 3; $i++){
        $possibilities[$i] = array();
        if(!isset($chunks[$i])){
          if($next == -1){
            $next = $i;
          }
          continue;
        }
        if(in_array($chunks[$i], $fontStyle)){
          $possibilities[$i] = 'font-style';
        }
        else if(in_array($chunks[$i], $fontVariant)){
          $possibilities[$i] = 'font-variant';
        }
        else if(in_array($chunks[$i], $fontWeight)){
          $possibilities[$i] = 'font-weight';
        }
        else if($chunks[$i] == 'normal'){
          $possibilities['normal'] = 1;
        }
        else if($next == -1){
          // Used to know where other properties will start at
          $next = $i;
        }
      }
    
      // Second pass to determine what real properties are defined
      for($i = 0; $i < 3; $i++){
        if(!empty($possibilities[$i])){
          $details[$possibilities[$i]] = $chunks[$i];
        }
      }
    
      // Third pass to set the properties which have to be set as "normal"
      if(!empty($possibilities['normal'])){
        $properties = array('font-style', 'font-variant', 'font-weight');
        foreach($properties as $property){
          if(!isset($details[$property])){
            $details[$property] = 'normal';
          }
        }
      }
    
      if(!isset($chunks[$next])){
        return $details;
      }
    
      // Extract font-size and line height
      if(strpos($chunks[$next], '/')){
        $size = explode('/', $chunks[$next]);
        $details['font-size'] = $size[0];
        $details['line-height'] = $size[1];
        $details['font-family'] = $chunks[$next+1];
      }
      else if(preg_match('`^-?[0-9]+`', $chunks[$next]) || 
              in_array($chunks[$next], array('xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'larger', 'smaller'))){
        $details['font-size'] = $chunks[$next];
        $details['font-family'] = $chunks[$next+1];
      }
      else{
        $details['font-family'] = $chunks[$next];
      }
    
      return $details;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following CSS: body { font-family: Corbel, Calibri, Verdana, Helvetica, sans-serif; font-size: 9pt;'
I'm facing a strange problem with CSS overriding of 'font-weight' property. Given below is
This is the closest I've seen: Changing font-size with jQuery css() function is crashing
What is the simplest way to extract css selectors as strings? Given this as
Question: Is there a way to check if a given font is one of
How can get the LineHeight of a Font given the FontSize? It seems that
I want to give my graph a title in big 18pt font, then a
Given this markup: // Calendar.html?date=1/2/2003 <script> $(function() { $('.inlinedatepicker').datepicker(); }); </script> ... <div class=inlinedatepicker
Given: unsigned int a, b, c, d; I want: d = a * b
Given this HTML: <div id=flashcard-001 class=flashcard> <div class=question>What color is the sky?</div> <div class=answer>blue</div>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.