Truyền tham số trong URL PHP

In reply to adrian,

Thank you very much for your function. There is a small issue with your relative protocol function. You need to remove the // when making the url the path. Here is the new function.

function resolve_url($base, $url) {
        if (!strlen($base)) return $url;
        // Step 2
        if (!strlen($url)) return $base;
        // Step 3
        if (preg_match('!^[a-z]+:!i', $url)) return $url;
        $base = parse_url($base);
        if ($url{0} == "#") {
                // Step 2 (fragment)
                $base['fragment'] = substr($url, 1);
                return unparse_url($base);
        }
        unset($base['fragment']);
        unset($base['query']);
        if (substr($url, 0, 2) == "//") {
                // Step 4
                return unparse_url(array(
                        'scheme'=>$base['scheme'],
                        'path'=>substr($url,2),
                ));
        } else if ($url{0} == "/") {
                // Step 5
                $base['path'] = $url;
        } else {
                // Step 6
                $path = explode('/', $base['path']);
                $url_path = explode('/', $url);
                // Step 6a: drop file from base
                array_pop($path);
                // Step 6b, 6c, 6e: append url while removing "." and ".." from
                // the directory portion
                $end = array_pop($url_path);
                foreach ($url_path as $segment) {
                        if ($segment == '.') {
                                // skip
                        } else if ($segment == '..' && $path && $path[sizeof($path)-1] != '..') {
                                array_pop($path);
                        } else {
                                $path[] = $segment;
                        }
                }
                // Step 6d, 6f: remove "." and ".." from file portion
                if ($end == '.') {
                        $path[] = '';
                } else if ($end == '..' && $path && $path[sizeof($path)-1] != '..') {
                        $path[sizeof($path)-1] = '';
                } else {
                        $path[] = $end;
                }
                // Step 6h
                $base['path'] = join('/', $path);

        }
        // Step 7
        return unparse_url($base);
}

urlencode function and rawurlencode are mostly based on RFC 1738.

However, since 2005 the current RFC in use for URIs standard is RFC 3986.

Here is a function to encode URLs according to RFC 3986.

function myUrlEncode($string) {
    $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');
    $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]");
    return str_replace($entities, $replacements, urlencode($string));
}
?>

Các tham số từ một chuỗi URL có thể được truy xuất trong PHP bằng cách sử dụng các hàm parse_url() và parse_str()

Ghi chú. URL trang và các tham số được phân tách bằng dấu ?

hàm parse_url(). Hàm parse_url() được sử dụng để trả về các thành phần của URL bằng cách phân tích cú pháp URL đó. Nó phân tích cú pháp một URL và trả về một mảng kết hợp chứa các thành phần khác nhau của nó

cú pháp.  

parse_url( $url, $component = -1 )

hàm parse_str(). Hàm parse_str() được sử dụng để phân tích một chuỗi truy vấn thành các biến. Chuỗi được chuyển đến chức năng này để phân tích cú pháp có định dạng chuỗi truy vấn được chuyển qua URL

cú pháp.   

parse_str( $string, $array )

Tiếp cận. Phân tích cú pháp chuỗi URL bằng cách sử dụng hàm parse_url() sẽ trả về một mảng kết hợp có chứa các thành phần khác nhau (URL đã truyền) của nó. Truy vấn của mảng được trả về bởi hàm parse_url() chứa chuỗi truy vấn URL

Các ví dụ dưới đây sử dụng hàm parse_url() và parse_str() để lấy các tham số từ chuỗi URL.  

ví dụ 1.   

PHP




 

// Initialize URL to the variable

$url ='https://www.geeksforgeeks.org?name=Tonny';

parse_str( $string, $array )
0

parse_str( $string, $array )
1

parse_str( $string, $array )
2

parse_str( $string, $array )
3

parse_str( $string, $array )
4 =
parse_str( $string, $array )
6
parse_str( $string, $array )
7$url
parse_str( $string, $array )
9

 

Hi Tonny
0

Hi Tonny
1

Hi Tonny
2_______1_______7
parse_str( $string, $array )
4
Hi Tonny
5
Hi Tonny
6
Hi Tonny
7
Hi Tonny
8
parse_str( $string, $array )
9

parse_str( $string, $array )
0

Hi Amit your emailID is [email protected]
1

Hi Amit your emailID is [email protected]
2
Hi Amit your emailID is [email protected]
3
Hi Amit your emailID is [email protected]
4
Hi Tonny
8_______10_______5
Hi Amit your emailID is [email protected]
7
Hi Amit your emailID is [email protected]
8

 

Hi Amit your emailID is [email protected]
9

đầu ra.

Hi Tonny

 

ví dụ 2.  

PHP




 

// Initialize URL to the variable

$url =4;

parse_str( $string, $array )
0

parse_str( $string, $array )
1

parse_str( $string, $array )
2

parse_str( $string, $array )
3

parse_str( $string, $array )
4 =
parse_str( $string, $array )
6
parse_str( $string, $array )
7$url
parse_str( $string, $array )
9

 

Hi Tonny
0

Hi Tonny
1

Hi Tonny
2_______1_______7
parse_str( $string, $array )
4
Hi Tonny
5
Hi Tonny
6
Hi Tonny
7
Hi Tonny
8
parse_str( $string, $array )
9

parse_str( $string, $array )
0

Hi Amit your emailID is [email protected]
1

Hi Amit your emailID is [email protected]
2
Hi Amit your emailID is [email protected]
3
Hi Amit your emailID is [email protected]
4
Hi Tonny
8_______10_______5
Hi Amit your emailID is [email protected]
7=4=5
Hi Amit your emailID is [email protected]
4
Hi Tonny
8
Hi Tonny
5=9
Hi Amit your emailID is [email protected]
8

 

Hi Amit your emailID is [email protected]
9

đầu ra.

Hi Amit your emailID is [email protected]

 

PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo Hướng dẫn PHP và Ví dụ về PHP này

Làm cách nào để chuyển tham số trong URL PHP?

Các tham số từ một chuỗi URL có thể được truy xuất trong PHP bằng cách sử dụng các hàm parse_url() và parse_str() .

Làm cách nào tôi có thể chuyển tham số bài đăng trong URL?

Gửi thông số . add it directly to the URL or open Params and enter the name and value.

Làm cách nào để chuyển dữ liệu mảng trong URL trong PHP?

Bạn có thể sử dụng hàm serialize() và urlencode PHP tích hợp để truyền một mảng dưới dạng tham số URL . Hàm serialize() sẽ trả về một chuỗi bit cho đầu vào đã cho và urlencode sẽ mã hóa lại các giá trị cũng như các ký tự đặc biệt có sẵn trong đó.

Làm cách nào để đăng dữ liệu lên URL trong PHP?

Nếu bạn đang tìm cách đăng dữ liệu lên một URL từ chính mã PHP (không sử dụng biểu mẫu html) thì có thể thực hiện được với curl. It will look like this: $url = 'http://www.someurl.com'; $myvars = 'myvar1=' . $myvar1 .