una significa que página para pagina otra misma internos hipervinculo hacer externos enlazar enlaces enlace ejemplos dentro como codigo php regex preg-replace

php - significa - href html ejemplos



La mejor manera de hacer enlaces en bloque de texto (6)

Quiero:

Here is link: http://google.com And http://example.com inside. And another one at the very end: http://test.net

convertirse:

Here is link: <a href="http://google.com">http://google.com</a> And <a href="http://example.com">http://example.com</a> inside. And another one at the very end: <a href="http://test.net">http://test.net</a>

Parece una tarea trivial, pero no puedo encontrar una función de PHP que funcione. ¿Tienes alguna idea?

function make_links_clickable($text){ // ??? } $text = ''Here is link: http://google.com And http://example.com inside. And another one at the very end: http://test.net''; echo make_links_clickable($text);


Inspirado por la respuesta de Akarun, se me ocurrió esta función para manejar todos los protocolos y enlaces que comienzan solo con www.

function make_links($text, $class='''', $target=''_blank''){ return preg_replace(''!((http/:////|ftp/:////|https/:////)|www/.)([-a-zA-Zа-яА-Я0-9/~/!/@/#/$/%/^/&/*/(/)_/-/=/+/////?/./:/;/'/,]*)?!ism'', ''<a class="''.$class.''" href="//$3" target="''.$target.''">$1$3</a>'', $text); }

Esta función tiene parámetros opcionales para agregar nombres de clase en los enlaces y también un objetivo opcional para el enlace, por lo que se abren en una nueva ventana / pestaña ... de manera predeterminada, el parámetro abre enlaces a una nueva ventana / pestaña, pero si no desea hacerlo eso, puede cambiar el valor predeterminado o cambiar el valor al llamar a la función.


Intenta algo como esto:

function make_links_clickable($text) { return preg_replace (''/http:////[^/s]+/i'', "<a href=/"${0}/">${0}</a>", $text); } $result = make_links_clickable($text);


También inspirada en la respuesta de Akarun, la siguiente función se dirigirá a los enlaces solo al texto que aún no es un enlace. La funcionalidad agregada es verificar que no existe un enlace con el enlace de texto capturado en la cadena de destino:

function make_links_from_http($content) { // Links out of text links preg_match_all(''!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!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; }

Al realizar pruebas, noté que la función anterior falla en la línea # 5. Una función "más sucia" que hace el trabajo es la siguiente:

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а-яА-Я()0-9@:%_+.~#?&;//=]+)!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; }

Para el nuevo código, he usado el tutorial en: http://www.the-art-of-web.com/php/parse-links/


Use esto (funciona con los esquemas ftp, http, ftps y https):

function make_links_clickable($text){ return preg_replace(''!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i'', ''<a href="$1">$1</a>'', $text); }



function makeClickableLinks($text) { $text = html_entity_decode($text); $text = " ".$text; $text= preg_replace("/(^|[/n ])([/w]*?)([/w]*?:////[/w]+[^ /,/"/n/r/t<]*)/is", "$1$2<a href=/"$3/" >$3</a>", $text); $text= preg_replace("/(^|[/n ])([/w]*?)((www|wap)/.[^ /,/"/t/n/r<]*)/is", "$1$2<a href=/"http://$3/" >$3</a>", $text); $text= preg_replace("/(^|[/n ])([/w]*?)((ftp)/.[^ /,/"/t/n/r<]*)/is", "$1$2<a href=/"$4://$3/" >$3</a>", $text); $text= preg_replace("/(^|[/n ])([a-z0-9&/-_/.]+?)@([/w/-]+/.([/w/-/.]+)+)/i", "$1<a href=/"mailto:$2@$3/">$2@$3</a>", $text); $text= preg_replace("/(^|[/n ])(mailto:[a-z0-9&/-_/.]+?)@([/w/-]+/.([/w/-/.]+)+)/i", "$1<a href=/"$2@$3/">$2@$3</a>", $text); $text= preg_replace("/(^|[/n ])(skype:[^ /,/"/t/n/r<]*)/i", "$1<a href=/"$2/">$2</a>", $text); return $text; }

trabajar con:

www.ejemplo.com

https://www.example.com

http://www.example.com

wap.example.com

ftp.example.com

[email protected]

skype: ejemplo

mailto: [email protected]

atherprotocol: //example.com