duandi4815 2015-10-11 12:37
浏览 29
已采纳

PHP - 有没有办法计算为您的需求而设计的正则表达式(正则表达式)?

I'm trying to write the regex to search for image tags within a string, then to count them using either 'count' or 'substr_count'

EG: [image1], [image2] etc etc..

I'm an absolute rookie with regex so it's pretty confusing to me. Before anyone asks, yes I have done a little research into it, but I was just curious as to whether or not there is any shortcuts that are out there that myself and anyone else reading this may be missing out on.

I have also found the https://regex101.com/ however am clueless as to where to go from there.

My efforts for the example above are:

'~(\[image([\d])+\]~i'

and ..

'#[^a-z0-9.]#i'

If someone could show me how to search for the above, or point me in the right direction to a programming gold-mine it'd be more than appreciated.

Cheers, Rich

  • 写回答

2条回答 默认 最新

  • dongyan1625 2015-10-11 12:49
    关注

    Use a preg_match_all, i.e.:

    $string = <<< LOL
    some text with image1 and another
    part of that contains image2
    yet another image3
    LOL;
    
    preg_match_all('/(image\d+)/i', $string, $matches, PREG_PATTERN_ORDER);
    for ($i = 0; $i < count($matches[1]); $i++) {
        echo $matches[1][$i]."
    ";
    }
    

    OUTPUT:

    image1
    image2
    image3
    

    DEMO:

    http://ideone.com/cQymaV

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

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型