
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
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 −
example.com
And the output we want is with "http://" i.e. an actual link −
http://example.com
For this, you can use dot(.) notation and conditional match with preg_match().
Example
<!DOCTYPE html> <body> <?php function addingTheHTTPValue($stringValue) { if (!preg_match("~^(?:f|ht)tps?://~i", $stringValue)) { $stringValue = "http://" . $stringValue; } return $stringValue; } echo addingTheHTTPValue("example.com"); echo "<br>"; echo addingTheHTTPValue("https://example.com"); ?> </body> </html>
Output
http://example.com https://example.com
- Related Articles
- Adding a column that doesn't exist in a query?
- MySQL query to include more than one column in a table that doesn't already exist
- If air pressure acting on chair then why it doesn't get crushed?
- Why doesn't the height of a container element increase if it contains floated elements?
- How a magnet attracts iron ? And it doesn't attract insulator. Why?
- PHP http://
- HTTP basic authentication URL with “@” in password.
- New line separator doesn't work for group_concat function in MySQL? How to use it correctly?
- How to plot a remote image from http url using Matplotlib?
- How to create a folder if it does not exist in C#?
- If sandy soil doesn't support the growth of plants, then how do plants such as coconut and palm tree grow in it?
- How to add a parameter to the URL in JavaScript?
- Python3 - Why loop doesn't work?
- Why doesn't JavaScript support multithreading?
- PHP HTTP context options

Advertisements