With more and more websites move to secure connection through HTTPS protocol, there may be a need to convert and replace all reference to unsecured HTTP protocol to SSL secured HTTPS protocol when referencing or linking to URLs.

When manipulating or dealing with URL in PHP programming language, it’s easier to replace all instances of http:// to https:// whenever required, such as when output HTML for web page, when validating the form, or when inserting or updating to the database.

To replace all references to http in PHP to https, use the preg_replace function with the following syntax:

$url = preg_replace("/^http:/i", "https:", $url);