简体   繁体   中英

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".

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".

Try,

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

DEMO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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