Created
July 13, 2015 19:28
-
-
Save webkader/416e682d3278b220da1d to your computer and use it in GitHub Desktop.
replace any links that don't start with http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// replace any links that don't start with http | |
if (preg_match_all('#<a href="([^"]+)"#', $text, $matches)) { | |
foreach ($matches[1] as $key => $match) { | |
if (!preg_match('#^https?:#', $match) && !preg_match('#^ftps?:#', $match)) { | |
$match = 'http://' . $match; | |
} | |
$text = preg_replace('#' . preg_quote($matches[0][$key], '#') . '#', '<a href="' . $match . '"', $text, 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment