你想再次爆炸你的结果字符串-
并保留第一个元素,类似于
$url = $_SERVER['REQUEST_URI'];
$check = end(explode('/local/',$url));
$check = current(explode('-', strtoupper($check)));
echo $check;
这样,如果最后一部分发生了变化,你仍然有你的CITYNAME
有效。
Some more explanation, explode
(documentation here) will break the string into array elements, and it will break it using the first parameter, so in this case, passing -
will create an array with 3 elements, CITYNAME
, FREE
and SERVICES
. current
(documentation here) will take the current position in the array and return this value, by default, the current position is on the first element. You can also index individual elements in your array, so explode('-', strtoupper($check))[0]
would also give you CITYNAME
, and using [1]
and [2]
would give you FREE
and SERVICES
.
Edit: I didn't see the dash part about city names. This complicates a bit the problem as your URL contains other dashes that you want to get rid of. If "-FREE-SERVICES" is constant and that's always the part you want to get rid of, then doing what cale_b suggested is a good idea, which is to replace "free-services"
with ""
. So I'm +1 his answer.
Edit 2: For some reason my answer was picked, for names with dashes you will need to do something similar to what cale_b suggested!