简体   繁体   English

检测字符串是否以 php 中的 x 位数字开头

[英]Detect if string begins by x digits number in php

How can I rename file if it begins by 5 digits?如果文件以5位数字开头,如何重命名文件?

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.这将匹配任何以五个或更多数字开头的文件名,并仅替换为前导数字,后跟扩展名。

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

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