请告诉我如何从谷歌字体网址preg_match字体名称。
例如,我想从以下位置提取字体名称:
http://fonts.googleapis.com/css?family=Oswald:400,300
http://fonts.googleapis.com/css?family=Roboto+Slab为了得到字体名称Oswald和Roboto Slab。
发布于 2013-05-04 17:48:48
这是一个你可以用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-04 17:42:19
您可以避免regexp的
$parsedUrl = parse_url($url);
$queryString = $parsedUrl['query'];
$parsedQueryString = parse_str($queryString);
$fontName = array_shift(explode(':', $parsedQueryString['family']));
$idealFontName = urldecode($fontName);
echo $idealFontName;https://stackoverflow.com/questions/16372745
复制相似问题