1

请告诉我如何preg_match从 Google 字体 URL 中使用字体名称。

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

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

为了获得字体名称OswaldRoboto Slab.

4

2 回答 2

3

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;
于 2013-05-04T09:42:19.037 回答
1

这是您可以使用preg_replace()执行的操作的示例,但是要小心谷歌的数据挖掘。

<?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;

}

?>
于 2013-05-04T09:48:47.507 回答