2013-05-04 09:28

使用正则表达式从Google Fonts URL获取字体名称


Please tell me how to preg_match font names from the Google Fonts URL.

For example, I want to extract the font name from:,300

in order to get the font names Oswald and Roboto Slab.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dtt83024 dtt83024 8年前

    Here's an example of what you might do with preg_replace(), however be careful data mining Google.

    $urls = array(",300",
    $patterns = array(
          //replace the path root
          //capture the family and avoid and any following attributes in the URI.
          //delete the variable name
          //replace the plus sign
    $replacements = array(
    ' ');
    foreach($urls as $url){
        $font = preg_replace($patterns,$replacements,$url);
        echo $font;
    点赞 评论 复制链接分享
  • doutang6819 doutang6819 8年前

    You can avoid regexp's

    $parsedUrl = parse_url($url);
    $queryString =  $parsedUrl['query']; 
    $parsedQueryString = parse_str($queryString);
    $fontName = array_shift(explode(':', $parsedQueryString['family']));
    $idealFontName = urldecode($fontName);
    echo $idealFontName;
    点赞 评论 复制链接分享