Friday, 13 September 2013

how to convert preg_replace http:// & www

how to convert preg_replace http:// & www

Here i have 3 type of links users gives in their $description.
www.Apple.com
http://www.Apple.com
http://apple.com
First i'm using this to make $description safe
$description = preg_replace('<[^A-Za-z0-9-.?#%=_&():/]>', '',
"$description");
Second I did this but here i got stuck.
$description = preg_replace('/(www.[^\s]*)/i', '<a href="//$1"
target="_blank">$1</a>', $description); //This converts www.apple.com
$description = preg_replace('/(http[s]?:\/\/[^\s]*)/i', '<a href="$1"
target="_blank">$1</a>', $description); // This converts http://apple.com
www. works and http:// doesn't works because they both combine together so
is there any process by which this can be undone Also Please tell me if
there is something wrong i have coded which can be harmful or better way
to do this.

No comments:

Post a Comment