douzhuo8312 2013-06-17 11:12
浏览 17
已采纳

如何在regexp子掩码中允许多个值

I want to pull some values out of string (in function)

if(!preg_match('/(.+)\.([0-9]*)x([0-9]*)(w|wr|r)?\.([^\.]+)$/', $filename, $matches))
    return false;

$file = $matches[1];
$width = $matches[2];
$height = $matches[3];
$set_watermark_or_nonconstrain = $matches[4];   // that's what I need to fix
$ext = $matches[5];

However, if I set string like {$image[0]->filename|resize:614:300:r} (Smarty), all goes well except that $set_watermark_or_nonconstrain is always 'w'. What do I need to set instead of w|wr|r to pull 'w', 'wr' or 'r' from this submask? Thanks!

  • 写回答

1条回答 默认 最新

  • dongwei5794 2013-06-17 13:10
    关注

    Using the string example you have given, the following expression does what you are looking for

       $string = '{$image[0]->filename|resize:614:300:r}';
    
       if(preg_match('/\{([^\|]+)\|[^:]+:(\d+):(\d+):([^}]+)/', $string, $m)){  
        echo 'm1: '.$m[1] ."<br />
    ";
        echo 'm2: '.$m[2] ."<br />
    ";
        echo 'm3: '.$m[3] ."<br />
    ";
        echo 'm4: '.$m[4] ."<br />
    ";
       }
    

    Which produces the following ouput:

    m1: $image[0]->filename
    m2: 614
    m3: 300
    m4: r
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?