< img src='image /ad3.gif' width="128" height=&q"/> < img src='image /ad3.gif' width="128" height=&q">

首頁  >  文章  >  後端開發  >  求正則,取得圖片src的地址

求正則,取得圖片src的地址

WBOY
WBOY原創
2016-08-04 09:20:40841瀏覽





正则取上述代码内src的值
image/ad1.gif
image/ad2.gif
image/ad3.gif
image/ad4.gif
image/ad5.gif

回复内容:





正则取上述代码内src的值
image/ad1.gif
image/ad2.gif
image/ad3.gif
image/ad4.gif
image/ad5.gif

获取src的值str,然后 str.match(/image(S*)gif/)[0]

<code>$('img').each(function(index,item){
    console.log(item.src.match(/image(\S*)gif/)[0]);
})</code>

<code>/src=(?:pattern'|\"|\\\['"]|\\\)?(.*?)(?:pattern'|\"|\\\['"]|\\\)?\sw/</code>

我也不知道正不正确

求正則,取得圖片src的地址

<code>//如果你的src中肯定不包含空格的话可以用这个简单的办法,通常是ok的
preg_match_all('/<img src=([^ \t]+)/', $str, $_match);</code>

这样得到的内容会包含单引号什么的,对于带有反斜线的单双引号先去掉反斜线,然后trim一下就行了,例如

<code>foreach($_match[1] as str) {
    $str = str_replace("\\'", "'", $str);
    $str = str_replace('\\"', '"', $str);
    echo trim($str, '\'"');
}</code>

大致这样,具体的自己调试吧。不太复杂的正则匹配优先考虑关键字符来排除结果,比如本例中的空格及制表符。

http://tool.oschina.net/regex/

<code><img[^>]*?src=\\?["]?(.*?)["]?\s[^>]*?></code>

此处应该有红包

求正則,取得圖片src的地址

<code>$rule = '#<img\s+src=([^\s]*).*?\/>#';</code>

你可以试试:在线正则表达式生成工具

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn