Pages

Tuesday, January 24, 2012

Checking and prepending `http://` in a link

Sometimes users/site admins are required to submit some URL, e.g. Facebook fanpage url or Twitter URL - that is an outbound link.

But HTML anchor tag href attribute does not work properly without http:// at the beginning of the url string.

So, we should have a validity check. The below line can do the work and will add the http:// if user does not give it.

In PHP:

if( stristr( $url, 'http://' )===false ) $url = 'http://'.$url;

No comments :

Post a Comment