【问题标题】:Error in retriving data using GET method in PHP在 PHP 中使用 GET 方法检索数据时出错
【发布时间】:2020-07-22 21:08:09
【问题描述】:

我尝试通过 URL 发送数据,并且该数据包含特殊字符,例如:mustafa+hamid
当我尝试检索此数据时,结果将是“mustafa hamid”,这意味着 + 会自动替换为空格。
如何检索原始文本??
谢谢

【问题讨论】:

    标签: php url get


    【解决方案1】:

    以下字符

    # $ & + ,  / : ; = ? @ [ ]
    

    保留。如果您想通过 GET 以变量形式发送它们,您必须首先“url 编码”它们。您必须在 URL 中发送“mustafa%2Bhamid”而不是“mustafa+hamid”。

    您可以使用 urlencode() PHP 函数来查看上述每个字符需要如何编码。

    【讨论】:

    • 你能指导我如何使用 urlencode..假设我们有以下链接 example.com/test.php?d=mustafa+hamid 然后我如何使用 urldecode 来检索原始文本?
    • 我会引用自己的话:“您必须在 URL 中发送“mustafa%2Bhamid”而不是“mustafa+hamid”。”
    【解决方案2】:

    你可以使用javascript函数

    var encodedURL= "http://www.example.com/test.php?d=" + encodeURIComponent('mustafa+hamid');
    
    //output :http://www.example.com/test.php?d=mustafa%2Bhamid
    

    或者您可以简单地将 + 替换为 %2B,这是 + 的编码代码

    【讨论】:

    • 希望对您有所帮助
    【解决方案3】:

    我不能评论,否则我会评论的。 Phillip Petrov 给出的答案在技术上是正确的。

    这是一个例子

    $data = "mustafa+hamid";
    $urlEncoded = urlencode( $data );
    
    // To get the data back to its original value
    $originalValue = urldecode( $urlEncoded );
    

    PHP - urlencode()

    PHP - urldecode()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 2015-06-13
      • 2016-03-24
      • 1970-01-01
      • 2019-03-30
      相关资源
      最近更新 更多