douzhao7445 2009-11-16 05:21
浏览 220
已采纳

正则表达式只捕获JPG图像

I have this regex in PHP:

$regex = '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui';

It captures all image tag sources in a string, but I want to only capture JPG files. I've tried to mess around with (.*) but I've only proven that I suck at regex... Right now I'm filtering the array but feels too much like a hack when I can just do it straight up with a proper match.

  • 写回答

6条回答

  • dtest84004 2009-11-16 05:29
    关注

    Try this:

    $regex = '/<img ([^>]* )?src=[\"\']([^\"\']*\.jpe?g)[\"\']/Ui';
    

    I also removed the extra | in the character classes that was not needed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao