在字符串中查找特定的 numbers/positions

Find specific numbers/positions in string

我需要从这样的字符串中获取数字:

main-section1-1
...
main-section1-512
...
main-section10-12

起初我可能需要从字符串中提取字母:

preg_replace("/[^0-9-]+/i", "", $string);

...但是下一步是什么?

例如:

$string = 'main-section1-1';

预期结果:

$str1 = 1;
$str2 = 1;

或:

$str = array(1,1);

使用preg_match_all()

<?php
$string = "main-section1-1";
preg_match_all( "/[0-9]+/", $string, $match );
print_r($match);

// for main-section1-512, you will get 1 and 512 in $match[0]
?>

输出:

[akshay@localhost tmp]$ php test.php
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 1
        )

)

如果我没有误解你的问题,这对你有用https://eval.in/875419

$re = '/([a-z\-]+)(\d+\-\d+)/';
$str = 'main-section1-512';
$subst = '';

$result = preg_replace($re, $subst, $str);
list($str1,$str2) = explode('-',$result);
echo $str1;
echo "\n";
echo $str2