create href with `http://` if protocol is not explicit in link

This commit is contained in:
vnmedeiros 2019-06-18 16:35:28 -03:00
parent 06f355ee55
commit d12470bd86
2 changed files with 9 additions and 3 deletions

View File

@ -18,6 +18,8 @@ trait Formatter_Text {
public function make_clickable_links($text) {
$url = '~((www\.|http:\/\/www\.|http:\/\/|https:\/\/www\.|https:\/\/|ftp:\/\/www\.|ftp:\/\/|ftps:\/\/www\.|ftps:\/\/)[^"<\s]+)(?![^<>]*>|[^"]*?<\/a)~i';
$text = preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $text);
$text = str_replace('href="www.', 'href="http://www.', $text);
return $text;
}
}

View File

@ -155,9 +155,9 @@ class TestUtilities extends TAINACAN_UnitTestCase {
$text_multiple_links_expected = 'Lorem <a href="https://www.tainacan.org" target="_blank" title="https://www.tainacan.org">https://www.tainacan.org</a> ipsum dolor sit amet <a href="http://www.tainacan.org" target="_blank" title="http://www.tainacan.org">http://www.tainacan.org</a>' .
' <a href="ftp://www.teste.com.br" target="_blank" title="ftp://www.teste.com.br">ftp://www.teste.com.br</a> consectetur adipiscing elit. <a href="ftps://www.teste.com.br" target="_blank" title="ftps://www.teste.com.br">ftps://www.teste.com.br</a> Sed pharetra sapien quis nunc vulputate dictum.' .
' <a href="www.simple.com.br" target="_blank" title="www.simple.com.br">www.simple.com.br</a> ' .
' <a href="www.simple.com" target="_blank" title="www.simple.com">www.simple.com</a> ' .
' <a href="www.simple.org" target="_blank" title="www.simple.org">www.simple.org</a> ' .
' <a href="http://www.simple.com.br" target="_blank" title="www.simple.com.br">www.simple.com.br</a> ' .
' <a href="http://www.simple.com" target="_blank" title="www.simple.com">www.simple.com</a> ' .
' <a href="http://www.simple.org" target="_blank" title="www.simple.org">www.simple.org</a> ' .
' Pellentesque id //ww.lair.com.br of a <a href="http://wwwliar.com.br" target="_blank" title="http://wwwliar.com.br">http://wwwliar.com.br</a> euismod mauris. //pegadinha.com.br ';
$text_multiple_links_response = $text->make_clickable_links($text_multiple_links);
@ -168,6 +168,10 @@ class TestUtilities extends TAINACAN_UnitTestCase {
$text_multiple_links_response = $text->make_clickable_links($text_multiple_links);
$this->assertEquals($text_multiple_links_expected, $text_multiple_links_response);
$text_input = 'If you type only www.abc.com without the http the link.';
$text_input_expected = 'If you type only <a href="http://www.abc.com" target="_blank" title="www.abc.com">www.abc.com</a> without the http the link.';
$text_input_response = $text->make_clickable_links($text_input);
$this->assertEquals($text_input_expected, $text_input_response);
}
}