简体   繁体   English

照片名称的正则表达式无效

[英]Regular expression for photo name not working

I am trying to use preg_match to have every photo whose filename is gallery\\album\\U[digits].jpg be titled "Untitled". 我正在尝试使用preg_match将每个照片的文件名为gallery\\album\\U[digits].jpg标题为“Untitled”。

Here is the code: 这是代码:

foreach($photos as $photo){ 
  if (preg_match('.*U[0-9]*\.jpg',$photo)) {
    $title = "Untitled";
  }
  else {
    $title = basename($photo,".jpg");
  }
}

Any idea why this isn't matching? 知道为什么这不匹配吗? The title of any of these files ends up being "U2" or "U29". 任何这些文件的标题最终都是“U2”或“U29”。

Try, 尝试,

if (preg_match('@U[0-9]+.jpg@',$photo)) {

DEMO. DEMO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM