dongpu6141 2013-05-04 09:28
浏览 48
已采纳

使用正则表达式从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:

http://fonts.googleapis.com/css?family=Oswald:400,300
http://fonts.googleapis.com/css?family=Roboto+Slab

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

  • 写回答

2条回答 默认 最新

  • dtt83024 2013-05-04 09:48
    关注

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

    <?php
    $urls = array("http://fonts.googleapis.com/css?family=Oswald:400,300",
    "http://fonts.googleapis.com/css?family=Roboto+Slab");
    
    $patterns = array(
          //replace the path root
    '!^http://fonts.googleapis.com/css\?!',
          //capture the family and avoid and any following attributes in the URI.
    '!(family=[^&:]+).*$!',
          //delete the variable name
    '!family=!',
          //replace the plus sign
    '!\+!');
    $replacements = array(
    "",
    '$1',
    '',
    ' ');
    
    foreach($urls as $url){
        $font = preg_replace($patterns,$replacements,$url);
        echo $font;
    
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM