Created
July 7, 2015 23:18
-
-
Save amereservant/89eb59e09cea6eed0c89 to your computer and use it in GitHub Desktop.
Convert URLs To Links Without RegEx - PHP
This file contains hidden or 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
<?php | |
/** | |
* Convert URLs To Links Without RegEx | |
* | |
* @author David Miles | |
* @link https://gist.github.com/amereservant | |
* | |
* After trying several RegEx patterns to try and match URLs and all of them failing to | |
* accurately match them, I decided to take a simplier approach. Since URLs are continuous | |
* strings and begin with a given string (http in this case), All I had to do was use simple | |
* string functions to identify the beginning and end of each URL, then replace them accordingly. | |
* | |
* It may be possible to simplify these and I will update this GIST if anyone offers such a revision, | |
* but my hope is it helps those who have been frustrated with regex patterns not working and this | |
* works great for them. | |
*/ | |
/** | |
* Get URLs From String | |
* | |
* This function takes the given string and searches for the beginning of a URL ('http'), | |
* starting at the $start position in that string. If it finds a match, then it will | |
* try to determine the end of the URL as such: | |
* 1) A blank space proceeding the URL. If one is found, it will filter for a newline | |
* character since one might appear after it and correct the string accordingly. | |
* 2) The end of the given string. If the url extends to the end of the string, | |
* this will be used to determine the end. | |
* | |
* This function should only be called in a loop by the parse_links function. | |
* | |
* @param string $content The string to search for urls in | |
* @param integer $start An offset to determine where to begin searching for the | |
* url in the string. | |
* @return array|bool An array with the URL and end position | |
*/ | |
function _getURLs($content, $start) | |
{ | |
// Look for the beginning of an URL | |
if( ($begin = strpos($content, 'http', $start)) !== FALSE ) | |
{ | |
// Look for a space following the URL | |
$end = strpos($content, ' ', $begin); | |
// If a space was found ... | |
if( $end != 0 ) | |
{ | |
$string = substr($content, $begin, ($end - $begin)); | |
// Filter for newline characters | |
$maybe_nl = explode("\n", $string); | |
if( count($maybe_nl) > 1 ) | |
{ | |
// The URL *should* be the first parameter and trimmed for use | |
$string = $maybe_nl[0]; | |
// Reduce the end position value for all additional characters that | |
// may proceed the newline character | |
for($i=1;$i < count($maybe_nl);$i++) | |
$end = $end - strlen($maybe_nl[$i]); | |
} | |
return array($string, $end); | |
} else { | |
return array(substr($content, $begin, (strlen($content)-$begin)), strlen($content)); | |
} | |
} | |
// No (more) URLs in the string, return false | |
return false; | |
} | |
/** | |
* Parse Links | |
* | |
* Takes the given string and locates the urls in the string, then replaces them with | |
* proper HTML tags to make them hyperlinks. | |
* | |
* @param string $content The content to convert urls into hyperlinks in | |
* @return string The input string with urls converted into hyperlinks | |
*/ | |
function parse_links($content) | |
{ | |
$urls = array(); // Store found URLs here so they can be replaced | |
$start = 0; | |
// Loop over the string and find all urls | |
while( (($url = _getURLs($content, $start)) !== FALSE) && ($start < strlen($content)) ) | |
{ | |
$urls[] = $url[0]; | |
$start = $url[1]; | |
} | |
// Replace all found URLs with HTML tags | |
foreach($urls as $url) | |
$content = str_replace($url, sprintf('<a href="%s" target="_blank">%1$s</a>', $url), $content); | |
return $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment