 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements
 