Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中将数字串成数字_Php_String_Numbers - Fatal编程技术网

在PHP中将数字串成数字

在PHP中将数字串成数字,php,string,numbers,Php,String,Numbers,假设我有一根像约翰利马一样的绳子 有没有办法将这两个数字提取出来,并将它们作为可以用$number++递增的数字?如果您的格式始终为'YYYYMMDD00.pdf',我将执行以下小功能: function increment_filename($incoming_string) { // The RegEx to Match $pattern = "/[0-9]{2}(?=\.pdf$)/i"; // Find where it matches preg_matc

假设我有一根像约翰利马一样的绳子


有没有办法将这两个数字提取出来,并将它们作为可以用$number++递增的数字?

如果您的格式始终为'YYYYMMDD00.pdf',我将执行以下小功能:

function increment_filename($incoming_string)
{
    // The RegEx to Match
    $pattern = "/[0-9]{2}(?=\.pdf$)/i";
    // Find where it matches
    preg_match($pattern, $incoming_string, $matches);
    // Replace and return the match incremented
    return preg_replace($pattern, $matches[0] + 1, $incoming_string);
}
如果您需要它匹配任何文件扩展名,这应该可以:

function increment_filename($incoming_string)
{
    // The RegEx to Match
    $pattern = "/[0-9]{2}(?=\.[a-z]+$)/i";
    // Find where it matches
    preg_match($pattern, $incoming_string, $matches);
    // Replace and return the match incremented
    return preg_replace($pattern, $matches[0] + 1, $incoming_string);
}

希望这会有所帮助,这是磨练我正则表达式技能的绝佳练习。:)

如果字符串中有两个数字呢<代码>John01Lima2这永远不可能吗?或者应该删除第二个数字?你可以很容易地从字符串中提取一个数字。我认为答案取决于你能保证字符串格式的具体程度。如果你知道它总是类似于你给出的例子,那么是的,它是可行的。字符串总是像这样的YYYYMMDD00.pdf,其中YYYYMMDD是日期,00是我需要访问和递增的数字。实际上,我现在想起来,它也应该在preg_匹配上有一个条件语句。如果字符串恰好不匹配,则尝试访问匹配数组时不会出现错误。