php - Add http:// to a link if it doesn't have it -
i have simple url bbcoder wish adjust if linked not contain http:// add in, how can this?
$find = array( "/\[url\=(.+?)\](.+?)\[\/url\]/is", "/\[url\](.+?)\[\/url\]/is" ); $replace = array( "<a href=\"$1\" target=\"_blank\">$2</a>", "<a href=\"$1\" target=\"_blank\">$1</a>" ); $body = preg_replace($find, $replace, $body);
you can use (http://)?
match http://
if exists, , ignore group result in 'replace to' pattern , use own http://
, this:
$find = array( "/\[url\=(http://)?(.+?)\](.+?)\[\/url\]/is", "/\[url\](http://)?(.+?)\[\/url\]/is" ); $replace = array( "<a href=\"http://$2\" target=\"_blank\">$3</a>", "<a href=\"http://$2\" target=\"_blank\">$2</a>" ); $body = preg_replace($find, $replace, $body);
Comments
Post a Comment