本主题是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?,我们应该把它抛给一些新的变量。
想要使用regex,尝试这个(不起作用):
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
)不需要数组中的第一个值。
谢谢。
发布于 2010-08-18 10:20:31
[^"]+这个词是贪婪的。若要使其在到达'后立即停止匹配,请使用[^"]+?。
我猜你是说[^']+吧。写这篇文章也会解决你的问题。
最后,将$src分配给$src[1]以获得第一个分组表达式。
https://stackoverflow.com/questions/3511016
复制相似问题