首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用正则表达式从Google字体URL获取字体名称

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

Stack Overflow用户
提问于 2013-05-04 17:28:42
回答 2查看 1K关注 0票数 1

请告诉我如何从谷歌字体网址preg_match字体名称。

例如,我想从以下位置提取字体名称:

代码语言:javascript
复制
http://fonts.googleapis.com/css?family=Oswald:400,300
http://fonts.googleapis.com/css?family=Roboto+Slab

为了得到字体名称OswaldRoboto Slab

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-04 17:48:48

这是一个你可以用preg_replace()做什么的例子,但是要小心挖掘谷歌的数据。

代码语言:javascript
复制
<?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
EN

Stack Overflow用户

发布于 2013-05-04 17:42:19

您可以避免regexp的

代码语言:javascript
复制
$parsedUrl = parse_url($url);
$queryString =  $parsedUrl['query']; 
$parsedQueryString = parse_str($queryString);
$fontName = array_shift(explode(':', $parsedQueryString['family']));
$idealFontName = urldecode($fontName);
echo $idealFontName;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16372745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档