Inoltre, ispirata alla risposta di Akarun, la seguente funzione converte i collegamenti solo nel testo che non è già un collegamento. La funzionalità aggiuntiva sta verificando che un legame con il link di testo catturato non esiste già nella stringa di destinazione:
function make_links_from_http($content) {
// Links out of text links
preg_match_all('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()[email protected]:%_+.~#?&;//=]+)!i', $content, $matches);
foreach ($matches[0] as $key=>$link) {
if (!preg_match('!<a(.*)'.$link.'(.*)/a>!i', $content))
{
$content = str_replace($link, '<a href="'.$link.'" target="_blank">'.$link.'</a>', $content);
}
}
return $content;
}
Con il test, ho notato che la funzione di cui sopra non riesce in linea # 5. Una funzione "Messier" che fa il lavoro è il seguente:
function make_links_from_http($content)
{
// The link list
$links = array();
// Links out of text links
preg_match_all('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()[email protected]:%_+.~#?&;//=]+)!i', $content, $matches);
foreach ($matches[0] as $key=>$link)
{
$links[$link] = $link;
}
// Get existing
preg_match_all('/<a\s[^>]*href=([\"\']??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU', $content, $matches);
foreach ($matches[2] as $key=>$value)
{
if (isset($links[$value]))
{
unset($links[$value]);
}
}
// Replace in content
foreach ($links as $key=>$link)
{
$content = str_replace($link, '<a href="'.$link.'" target="_blank">'.$link.'</a>', $content);
}
return $content;
}
Per il nuovo codice, ho usato il tutorial: http://www.the-art-of-web.com/php/parse-links/
fonte
2015-11-18 18:25:56
Domanda duplicata; Vedere a http://stackoverflow.com/questions/5282745/simple-wiki-parser-and-link-autodetection – Akarun
Eventuali duplicati di [sostituire gli URL nel testo con link HTML] (http://stackoverflow.com/questions/1188129/replace-urls-in-text-with-html-links) – cweiske