写一个php函数,尽可能高效的,从一个url中提取jpg文件的文件名

该字符串如下:
www.testweb.com/abc13.class.jpg" target="_blank">http://www.testweb.com/abc13.class.jpg

第1个回答  推荐于2017-09-05
<?php 

function getJpgName($str) {
$reg = "([^\\/]+\\.jpg)";
preg_match($reg, $str, $result);
if (isset($result[0])) {
return $result[0];
} else {
return "";
}
}

echo getJpgName("tweb.com/abc13.class.jpg\" target=\"_blank\">
);

?>

没考虑高效的事,直接用正则了。。

本回答被网友采纳
第2个回答  2017-09-05

$text = '
<a href="www.testweb.com/abc13.class.jpg" target="_blank">
<a href="www.testweb.com/abc13.class.jpg" target="_blank">
';

$pattern = '/href\s?=\s?[\'|"]([^\'|"]*)[\'|"]/i';
var_dump(preg_get($pattern , $text));

function preg_get($pattern , $text)
{
$out = array();
preg_match_all( $pattern , $text, $out );
return $out[1];
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网