There are two ways of tackling this problem: url parsing and regular expressions.

Some will say url parsing is right, but regular expressions work just as well in this case. I like being able to have simple one-liners for things like this especially because this would be a common occurrence in template files where you may need a one-liner inside an echo statement to maintain readability.

Regular Expressions

This uses a negative lookahead at the beginning of the string that looks for http:// or https://. If either are found, the replace doesn't happen. If they aren't found, it replaces the beginning of the string (0 characters) with http:// essentially prepending this to the string without modifying it.

URL Parsing

What this does is find out if a scheme is present on the link throught parse_url($aProfileInfo['Website'], PHP_URL_SCHEME). Then using a ternary operator, it will either output '' if there was one found or 'http://' if one wasn't found. Then it appends the link onto that.

Your regex is smart that it preserves https:// however it doesn't work with relative urls such as //www.example.com. It also appends http:// onto ftp:// or other protocols. Try this instead: preg_replace('/^\/\/|^(?!https?:)(?!ftps?:)/', 'http://', $src) (note: you can remove the check for ftp if you dont need it)
– dhaupinMay 13 '16 at 16:05