PHP - 获取字符串中单词后的整数

PHP - Get integer after word inside string

有这个 URL 字符串:

$URL = "www.example.com/search/brand/model/priceRange:2000-5000/year:1994-2015";

如何确定价格范围和年份?所以我的最终变量导致这个:

$price_from = 2000;
$price_until= 5000;

$year_from = 1994;
$year_until= 2015;

阅读了几篇文章后,我已经尝试使用 explode() 方法,但我不确定如何使用这样的字符串,在此先感谢

编辑: 忘了说URL里面的元素顺序是可以改的,谢谢

您可以尝试这样的操作:

$result = array();
$url_parts = explode('/', $URL);
foreach ($url_parts as $part) {
    if (strpos($part, ':') && strpos($part, '-')) {
       $sub = explode(':', $part);
       $range = explode('-', $sub[1]);
       $result[$sub[0].'_from'] = $range[0];
       $result[$sub[0].'_until'] = $range[1];
    }
}

demo