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

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -