使用 preg_replace_callback() 从 HTML 字符串中提取所有图像
这里有棘手的 preg_replace_callback 函数 - 不可否认,我不擅长 PRCE 表达式。
我试图从 HTML 字符串中提取所有 img src 值,将 img src 值保存到数组中,并另外将 img src 路径替换为本地路径(而不是远程路径)。即我可能有很多其他 HTML:
img src='http://www.mysite.com/folder/subfolder/images/myimage.png'
我想将 myimage.png 提取到一个数组中,另外将 src 更改为:
src='images/myimage.png'
可以吗?
谢谢
Tricky preg_replace_callback function here - I am admittedly not great at PRCE expressions.
I am trying to extract all img src values from a string of HTML, save the img src values to an array, and additionally replace the img src path to a local path (not a remote path). Ie I might have, surrounded by a lot of other HTML:
img src='http://www.mysite.com/folder/subfolder/images/myimage.png'
And I would want to extract myimage.png to an array, and additionally change the src to:
src='images/myimage.png'
Can that be done?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是否需要使用正则表达式?使用 DOM 函数处理 HTML 通常更容易:
Does it need to use regular expressions? Handling HTML is normally easier with DOM functions:
你需要正则表达式吗?没有必要。正则表达式是最具可读性的解决方案吗?可能不会——至少除非你精通正则表达式。扫描大量数据时正则表达式是否更有效?当然,正则表达式在第一次出现时就会被编译和缓存。正则表达式赢得“最少行代码”奖杯吗?
两行代码,在 PHP 中很难削弱。它会生成以下
$images
数组:请注意,这不适用于 5.3 之前的 PHP 版本,除非您将匿名函数替换为正确的函数。
Do you need regex for this? Not necessary. Are regex the most readable solution? Probably not - at least unless you are fluent in regex. Are regex more efficient when scanning large amounts of data? Absolutely, the regex are compiled and cached upon first appearance. Do regex win the "least lines of code" trophy?
Two lines of code, that's hard to undercut in PHP. It results in the following
$images
array:Please note that this won't work with PHP versions prior to 5.3 unless you replace the anonymous function with a proper one.