简体   繁体   中英

Detect if string begins by x digits number in php

How can I rename file if it begins by 5 digits?

This is what I tried:

$filename = '156272_abc.png';
$string = PREG_REPLACE("/[^0-9]i5/", '', $filename);

But it doesn't work.

Could you please point me in the right direction?

Thanks.

I think what you want is this:

$filename = '156272_abc.png';
$replacement = preg_replace("/^(\d{5,}).*(\.[^.]+)$/", "$1$2", $filename);

This will match any filename beginning with five or more digits, and replace with just the leading digits, followed by the extension.

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