我需要从这样的字符串中获取数字:
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);
答案 0 :(得分:2)
<?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
)
)
答案 1 :(得分:1)
如果我没有误解你的问题,这对你有用https://eval.in/875419
$re = '/([a-z\-]+)(\d+\-\d+)/';
$str = 'main-section1-512';
$subst = '$2';
$result = preg_replace($re, $subst, $str);
list($str1,$str2) = explode('-',$result);
echo $str1;
echo "\n";
echo $str2