PHP parses the URL and loses the parameters in the URL

Source: Internet
Author: User
PHP parses the URL and gets the parameters in the URL

!--? php// Example of a URL-formatted string: $STR = ' HTTP://TEST.COM/TESTDIR/INDEX.PHP?PARAM1=10&PARAM2=20&PARAM3=30&PARAM4=40&PARAM5=50&PARAM6 =60 ';//1 uses Parse_url to parse the URL, here is $str$arr = Parse_url ($STR); Var_dump ($arr);//2 takes a parameter out of the URL and puts it in the array $arr_query =    Converturlquery ($arr [' query ']); Var_dump ($arr _query);//3 the parameter array back to the parameter format in string form Var_dump (Geturlquery ($arr _query));/** * Returns the URL query as associative array * @param string query * @return Array params */fun    Ction Converturlquery ($query) {$queryParts = explode (' & ', $query);    $params = Array ();        foreach ($queryParts as $param) {$item = explode (' = ', $param);    $params [$item [0]] = $item [1]; } return $params;}    function Geturlquery ($array _query) {$tmp = array ();    foreach ($array _query as $k =--> $param) {$tmp [] = $k. ' = '. $param;    } $params = Implode (' & ', $tmp); return $params;}? 

Output Result:

Array (size=4)  ' scheme ' = = String ' http ' (length=4)  ' host ' = = String ' test.com ' (length=8)  ' path ' = > String '/testdir/index.php ' (length=18)  ' query ' = = String ' param1=10&param2=20&param3=30& Param4=40&param5=50&param6=60 ' (length=59) array (size=6)  ' param1 ' = = String ' (length=2)  ' Param2 ' = ' + String ' (length=2)  ' param3 ' + string ' (length=2)  ' param4 ' = = String ' (length=2) 
   ' PARAM5 ' + string ' (length=2)  ' param6 ' = = String ' (length=2) string ' param1=10&param2=20& Param3=30&param4=40&param5=50&param6=60 ' (length=59)


  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.