- http_build_url
- Parameters
- Return Values
- Examples
- See Also
- User Contributed Notes 3 notes
- How to add http:// if it doesn’t exists in the URL in PHP?
- How to add http:// if it doesn’t exists in the URL in PHP?
- How to add http:// if it doesn’t exist in the URL
- How to add http:// if it doesn’t exist in the URL PHP?
- Example
- Output
- How can I add http:// to a URL if no protocol is defined in JavaScript? [duplicate]
- Как добавить http: //, если он не существует в URL?
http_build_url
The parts of the second URL will be merged into the first according to the flags argument.
Parameters
(part(s) of) a URL in form of a string or associative array like parse_url() returns
same as the first argument
a bitmask of binary or’ed HTTP_URL constants; HTTP_URL_REPLACE is the default
if set, it will be filled with the parts of the composed url like parse_url() would return
Return Values
Returns the new URL as string on success or FALSE on failure.
Examples
Example #1 A http_build_url() example
echo http_build_url ( «http://user@www.example.com/pub/index.php?a=b#files» ,
array(
«scheme» => «ftp» ,
«host» => «ftp.example.com» ,
«path» => «files/current/» ,
«query» => «a=c»
),
HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT
);
?>?php
The above example will output:
ftp://ftp.example.com/pub/files/current/?a=c
See Also
User Contributed Notes 3 notes
pecl_http 2+ won’t provide http_ functions any more. They moved to Http namespace, sadly no backwards compat.
To sum it up — As of pecl_http >=2.0.0 this is no longed available. Also pecl_http 1.7.6 will work only for PHP
It seems to me that the return value must always have a protocol, a host and a path. If they are not provided in the input, default values are added.
From what I saw, the default value for the protocol is ‘http://’, for the host is the hostname (if running from cli) or the variable $_SERVER[‘HTTP_HOST’], for the path is the variable $_SERVER[‘SCRIPT_NAME’]
The class that replaces the functionality of this function is «http\Url».
The official decomentation can be found at: https://mdref.m6w6.name/http/Url
- HTTP Functions
- http_cache_etag
- http_cache_last_modified
- http_chunked_decode
- http_deflate
- http_inflate
- http_build_cookie
- http_date
- http_get_request_body_stream
- http_get_request_body
- http_get_request_headers
- http_match_etag
- http_match_modified
- http_match_request_header
- http_support
- http_negotiate_charset
- http_negotiate_content_type
- http_negotiate_language
- ob_deflatehandler
- ob_etaghandler
- ob_inflatehandler
- http_parse_cookie
- http_parse_headers
- http_parse_message
- http_parse_params
- http_persistent_handles_clean
- http_persistent_handles_count
- http_persistent_handles_ident
- http_get
- http_head
- http_post_data
- http_post_fields
- http_put_data
- http_put_file
- http_put_stream
- http_request_body_encode
- http_request_method_exists
- http_request_method_name
- http_request_method_register
- http_request_method_unregister
- http_request
- http_redirect
- http_send_content_disposition
- http_send_content_type
- http_send_data
- http_send_file
- http_send_last_modified
- http_send_status
- http_send_stream
- http_throttle
- http_build_str
- http_build_url
How to add http:// if it doesn’t exists in the URL in PHP?
Let’s say we have passed the following value − And the output we want is with «http://» i.e. an actual link − For this, you can use dot(.) notation and conditional match with preg_match(). Here, we have set a function that adds «http:// to a string.
How to add http:// if it doesn’t exists in the URL in PHP?
There are many approaches to add http:// in the URL if it doesn’t exist. Some of them are discussed below.
Method 1: Using preg_match() function: This function searches string for pattern and returns true if pattern exists, otherwise returns false. Usually, the search starts from the beginning of the subject string. The optional parameter offset is used to specify the position from where to start the search.int preg_match($pattern, $string, $pattern_array, $flags, $offset )
Return value: It returns true if pattern exists, otherwise false.
Example 1: This example takes the URL without http:// and returns the complete URL.
geeksforgeeks.org http://geeksforgeeks.org
Example 2: This example takes the URL with http:// and returns the URL without doing any correction.
http://geeksforgeeks.org http://geeksforgeeks.org
Method 2: This method use parse_url() function to add http:// if it does not exist in the url.
geeksforgeeks.org http://geeksforgeeks.org
Method 3: This method use strpos() function to add the http:// if it does not exist in the url.
geeksforgeeks.org http://geeksforgeeks.org
Method 4: This method use parse_url() function to add http:// in the url if it does not exists.
geeksforgeeks.org http://geeksforgeeks.org
What is the best regular expression to check if a string is a valid URL?, @Gumbo, it’s allowed in the spec and used in URI implementations for HTTP applications. It’s discouraged (for obvious reasons) but perfectly valid and should be
How to add http:// if it doesn’t exist in the URL
How can I add http:// to a URL if it doesn’t already include a protocol (e.g. http:// , https:// or ftp:// )?
addhttp("google.com"); // http://google.com addhttp("www.google.com"); // http://www.google.com addhttp("google.com"); // http://google.com addhttp("ftp://google.com"); // ftp://google.com addhttp("https://google.com"); // https://google.com addhttp("http://google.com"); // http://google.com addhttp("rubbish"); // http://rubbish
A modified version of @nickf code:
function addhttp($url) < if (!preg_match("~^(?:f|ht)tps?://~i", $url)) < $url = "http://" . $url; >return $url; >
Recognizes ftp:// , ftps:// , http:// and https:// in a case insensitive way.
At the time of writing, none of the answers used a built-in function for this:
function addScheme($url, $scheme = 'http://') < return parse_url($url, PHP_URL_SCHEME) === null ? $scheme . $url : $url; >echo addScheme('google.com'); // "http://google.com" echo addScheme('https://google.com'); // "https://google.com"
Simply check if there is a protocol (delineated by «://») and add «http://» if there isn’t.
Note : This may be a simple and straightforward solution, but Jack’s answer using parse_url is almost as simple and much more robust. You should probably use that one.
The best answer for this would be something like this:
function addhttp($url, $scheme="http://" ) < return $url = empty(parse_url($url)['scheme']) ? $scheme . ltrim($url, '/') : $url; >
The protocol flexible, so the same function can be used with ftp, https, etc.
File_get_contents when url doesn’t exist, While this is a good solution, it doesn’t consider other http error codes like 500. So, a simple tweak could be like: $headers = get_headers($uri); if (stripos
How to add http:// if it doesn’t exist in the URL PHP?
Here, we have set a function that adds «http:// to a string. Let’s say we have passed the following value −
And the output we want is with «http://» i.e. an actual link −
For this, you can use dot(.) notation and conditional match with preg_match().
Example
return $stringValue; > echo addingTheHTTPValue("example.com"); echo "
"; echo addingTheHTTPValue("https://example.com"); ?>