首页 > 解决方案 > 确定 url 中的编码类型

问题描述

我正在解析具有以下形式的网址:

http:\/\/domain.com\/?key1=value1&key2=value2

您可以看到所有的/都被编码为\/和所有的&as &。很容易对这个替换进行硬编码以获得真正的 url,但可能有一些其他字符编码不同。我正在寻找这种编码的类型,以便我可以处理我目前无法预测的情况。你知道这个编码标准吗?

标签: htmljsonencoding

解决方案


只需对其进行逆向工程,以下是获取值所需的步骤。

$str = "http:\/\/domain.com\/?key1=value1&key2=value2";

$str = htmlspecialchars_decode($str);// Convert & to just &
$parts = parse_url($str); //Seperate Url string to its parts 
parse_str($parts['query'], $params); // Parse the parts to get the query params and set them to a new out &params 

//Loop though different params
foreach($params as $key => $value)
{
    echo $key . " = " . $value . "</br>"; 
}

//Output
key1 = value1
key2 = value2

推荐阅读