求一正则,替换img标签的src属性

WBOY
Release: 2016-06-23 13:24:43
Original
958 people have browsed it

如题,我的$data['content']里有很多内容,其中也有img标签:
求一正则,替换img标签的src属性


我想得到把src属性里的/Uploads/去掉,其他的保存不变。正则一直不怎么会,求大神告诉我怎么过滤,谢谢


回复讨论(解决方案)

preg_replace('`src="/Uploads/([^"]+?)"`','src="$1"',$data['content']);
Copy after login
Copy after login

preg_replace('`src="/Uploads/([^"]+?)"`','src="$1"',$data['content']);
Copy after login
Copy after login


谢谢,达到效果了。我想知道:正则里的    [^"]+?   是什么作用?

^ 除了 “ 都匹配,+ 至少一个,? 非贪婪(尽可能少的)匹配

就是匹配到后面正常的链接(相当于用 ” 来做匹配的结束),用$1表示捕获到的内容

 [^"] 不是 " 的所有字符 
+? 非贪婪匹配

用在这里多余了

 [^"] 不是 " 的所有字符 
+? 非贪婪匹配

用在这里多余了



问下,这个正则(src=".*(http://.*?)")会不会好点?
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template