【发布时间】:2011-03-31 11:23:09
【问题描述】:
此话题为PHP get image src的备注
所以我们有一个变量,里面有代码(只有图像):
$image = "<img src='http://www.gravatar.com/avatar/66ce1f26a725b2e063d128457c20eda1?s=32&d=identicon&r=PG' height='32' width='32' alt=''/>";
我们怎样才能得到这张图片的src?我们应该把它扔给一些新的变量。
想用正则表达式,试过这个(不行):
preg_match('@src=\'([^"]+)\'@', $image, $src);
问题是 - 有单引号而不是双引号。
最后,我们必须得到:
$src = 'http://www.gravatar.com/avatar/66ce1f26a725b2e063d128457c20eda1?s=32&d=identicon&r=PG';
搜索真正的正则表达式。
如果我使用'@src=\'([^"]+?)\'@',print_r($src) 给出:
Array (
[0] => src='http://www.gravatar.com/avatar/66ce1f26a725b2e063d128457c20eda1?s=32&d=identicon&r=PG'
[1] => http://www.gravatar.com/avatar/66ce1f26a725b2e063d128457c20eda1?s=32&d=identicon&r=PG
)
不需要数组中的第一个值。
谢谢。
【问题讨论】:
-
$src = $src[1] ?我不知道你的意思。使用 print_r( $src ) 查看匹配数组,并获得所需的值。