如何从字符串使用PHP获取特定的子字符串数字?
问题描述:
$fileName = "Backup_1486874341.tar.gz"
这是我的文件名。我怎样才能得到从该文件名只有时间戳喜欢1486874341
如何从字符串使用PHP获取特定的子字符串数字?
答
在preg_match()
使用简单的正则表达式来选择文件名中的目标数字。
$fileName = "Backup_1486874341.tar.gz";
preg_match("/_(\d+)./", $fileName, $matches);
echo $matches[1];
看看导致demo
答
下面是示例片段:
<?php
$str = 'Backup_1486874341.tar.gz';
preg_match_all('!\d+!', $str, $matches);
print_r($matches[0][0]);
?>
[从一个字符串提取号码](http://*.com/questions/6278296/extract-numbers -from-a-string) – Rizier123
尝试添加一些更多描述。 – Seeker
你是否尝试过自己解决这个问题? – kkaosninja