您好,我是PHP的新手,对于我的家庭项目,我想在URL上分割值的地方需要帮助。
$url = "http://localhost/Sub/Key/Key_XYZ_1234.php";
URL也可以是:
$url = "http://localhost/Sub/Key/Key-XYZ-1234.php";
键值可以是“ ABC-TT”或“ ABC_TT”,也可以只是“ ABC”
预期结果
$v1 = value of Key;
$v2 = value of XYZ;
$v3 = value of 1234;
预先感谢
答案 0 :(得分:0)
类似....
//this is psuedo code, but you can look up the function names to see how to use them
$path=parse_url($url, PHP_URL_PATH);
$sep="-";
if(strpos($path,$sep)===false){
$sep="_";
}
$pieces=explode($sep,$path);
$key=$pieces[0];
$a=$pieces[1];
$nums=$peices[2];
答案 1 :(得分:0)
您可以结合使用parse_url
,basename
和preg_split
来做到这一点。我们使用preg_split
,因此我们可以将分隔符处理为-
或_
:
$url = "http://localhost/Sub/Key/Key-XYZ-1234.php";
$path = parse_url($url, PHP_URL_PATH);
$file = basename($path, '.php');
list($v1, $v2, $v3) = preg_split('/[-_]/', $file);
echo "\$v1 = $v1\n\$v2 = $v2\n\$v3 = $v3\n";
输出:
$v1 = Key
$v2 = XYZ
$v3 = 1234