/** * Given plain text, makes it into HTML as nicely as possible. * May contain HTML tags already * * @uses $CFG * @param string $text The string to convert. * @param boolean $smiley Convert any smiley characters to smiley images? * @param boolean $para If true then the returned string will be wrapped in paragraph tags * @param boolean $newlines If true then lines newline breaks will be converted to HTML newline breaks. * @return string */ function text_to_html($text, $smiley = true, $para = true, $newlines = true) { /// global $CFG; /// Remove any whitespace that may be between HTML tags $text = eregi_replace(">([[:space:]]+)<", "><", $text); /// Remove any returns that precede or follow HTML tags $text = eregi_replace("([\n\r])<", " <", $text); $text = eregi_replace(">([\n\r])", "> ", $text); convert_urls_into_links($text); /// Make returns into HTML newlines. if ($newlines) { $text = nl2br($text); } /// Turn smileys into images. if ($smiley) { replace_smilies($text); } /// Wrap the whole thing in a paragraph tag if required if ($para) { return '<p>' . $text . '</p>'; } else { return $text; } }
function test_convert_urls_into_links() { $texts = array('http://moodle.org - URL' => '<a href="http://moodle.org" target="_blank">http://moodle.org</a> - URL', 'www.moodle.org - URL' => '<a href="http://www.moodle.org" target="_blank">www.moodle.org</a> - URL', 'URL: http://moodle.org/s/i=1&j=2' => 'URL: <a href="http://moodle.org/s/i=1&j=2" target="_blank">http://moodle.org/s/i=1&j=2</a>', 'URL: www.moodle.org/s/i=1&j=2' => 'URL: <a href="http://www.moodle.org/s/i=1&j=2" target="_blank">www.moodle.org/s/i=1&j=2</a>', 'URL: https://moodle.org/s/i=1&j=2' => 'URL: <a href="https://moodle.org/s/i=1&j=2" target="_blank">https://moodle.org/s/i=1&j=2</a>', 'URL: http://moodle.org:8080/s/i=1' => 'URL: <a href="http://moodle.org:8080/s/i=1" target="_blank">http://moodle.org:8080/s/i=1</a>', '(http://moodle.org) - URL' => '(<a href="http://moodle.org" target="_blank">http://moodle.org</a>) - URL', '(www.moodle.org) - URL' => '(<a href="http://www.moodle.org" target="_blank">www.moodle.org</a>) - URL', '[http://moodle.org] - URL' => '[<a href="http://moodle.org" target="_blank">http://moodle.org</a>] - URL', '[www.moodle.org] - URL' => '[<a href="http://www.moodle.org" target="_blank">www.moodle.org</a>] - URL', '[http://moodle.org/main#anchor] - URL' => '[<a href="http://moodle.org/main#anchor" target="_blank">http://moodle.org/main#anchor</a>] - URL', '[www.moodle.org/main#anchor] - URL' => '[<a href="http://www.moodle.org/main#anchor" target="_blank">www.moodle.org/main#anchor</a>] - URL', 'URL: http://cc.org/url_(withpar)_go/?i=2' => 'URL: <a href="http://cc.org/url_(withpar)_go/?i=2" target="_blank">http://cc.org/url_(withpar)_go/?i=2</a>', 'URL: www.cc.org/url_(withpar)_go/?i=2' => 'URL: <a href="http://www.cc.org/url_(withpar)_go/?i=2" target="_blank">www.cc.org/url_(withpar)_go/?i=2</a>', 'URL: http://cc.org/url_(with)_(par)_go/?i=2' => 'URL: <a href="http://cc.org/url_(with)_(par)_go/?i=2" target="_blank">http://cc.org/url_(with)_(par)_go/?i=2</a>', 'URL: www.cc.org/url_(with)_(par)_go/?i=2' => 'URL: <a href="http://www.cc.org/url_(with)_(par)_go/?i=2" target="_blank">www.cc.org/url_(with)_(par)_go/?i=2</a>', 'http://en.wikipedia.org/wiki/Slash_(punctuation)' => '<a href="http://en.wikipedia.org/wiki/Slash_(punctuation)" target="_blank">http://en.wikipedia.org/wiki/Slash_(punctuation)</a>', 'http://en.wikipedia.org/wiki/%28#Parentheses_.28_.29 - URL' => '<a href="http://en.wikipedia.org/wiki/%28#Parentheses_.28_.29" target="_blank">http://en.wikipedia.org/wiki/%28#Parentheses_.28_.29</a> - URL', 'http://en.wikipedia.org/wiki/(#Parentheses_.28_.29 - URL' => '<a href="http://en.wikipedia.org/wiki/(#Parentheses_.28_.29" target="_blank">http://en.wikipedia.org/wiki/(#Parentheses_.28_.29</a> - URL', 'http://en.wikipedia.org/wiki/Slash_%28punctuation%29' => '<a href="http://en.wikipedia.org/wiki/Slash_%28punctuation%29" target="_blank">http://en.wikipedia.org/wiki/Slash_%28punctuation%29</a>', 'URL: <a href="http://moodle.org">http://moodle.org</a>' => 'URL: <a href="http://moodle.org">http://moodle.org</a>', 'URL: <a href="http://moodle.org">www.moodle.org</a>' => 'URL: <a href="http://moodle.org">www.moodle.org</a>', 'URL: <a href="http://moodle.org"> http://moodle.org</a>' => 'URL: <a href="http://moodle.org"> http://moodle.org</a>', 'URL: <a href="http://moodle.org"> www.moodle.org</a>' => 'URL: <a href="http://moodle.org"> www.moodle.org</a>', 'URL: http://moodle.org/s/i=1&j=2.' => 'URL: <a href="http://moodle.org/s/i=1&j=2" target="_blank">http://moodle.org/s/i=1&j=2</a>.', 'URL: www.moodle.org/s/i=1&j=2.' => 'URL: <a href="http://www.moodle.org/s/i=1&j=2" target="_blank">www.moodle.org/s/i=1&j=2</a>.', 'URL: http://moodle.org)<br />' => 'URL: <a href="http://moodle.org" target="_blank">http://moodle.org</a>)<br />', 'URL: <p>text www.moodle.org</p> text' => 'URL: <p>text <a href="http://www.moodle.org" target="_blank">www.moodle.org</a></p> text', 'URL: www.moodle.org?u=1.23' => 'URL: <a href="http://www.moodle.org?u=1.23" target="_blank">www.moodle.org?u=1.23</a>', 'URL: www.moodle.org?u=test+param&' => 'URL: <a href="http://www.moodle.org?u=test+param&" target="_blank">www.moodle.org?u=test+param&</a>', 'URL: www.moodle.org?param=:)' => 'URL: <a href="http://www.moodle.org?param=:)" target="_blank">www.moodle.org?param=:)</a>', 'URL: http://moodle.org www.moodle.org' => 'URL: <a href="http://moodle.org" target="_blank">http://moodle.org</a> <a href="http://www.moodle.org" target="_blank">www.moodle.org</a>', 'URL: <a href="http://moodle.org">http://moodle.org</a> www.moodle.org <a class="customclass" href="http://moodle.org">http://moodle.org</a>' => 'URL: <a href="http://moodle.org">http://moodle.org</a> <a href="http://www.moodle.org" target="_blank">www.moodle.org</a> <a class="customclass" href="http://moodle.org">http://moodle.org</a>', 'http://subdomain.moodle.org - URL' => '<a href="http://subdomain.moodle.org" target="_blank">http://subdomain.moodle.org</a> - URL', 'http://subdomain.subdomain.moodle.org - URL' => '<a href="http://subdomain.subdomain.moodle.org" target="_blank">http://subdomain.subdomain.moodle.org</a> - URL', 'This contains http, http:// and www but no actual links.' => 'This contains http, http:// and www but no actual links.', 'This is a story about moodle.coming to a cinema near you.' => 'This is a story about moodle.coming to a cinema near you.', 'http://Iñtërnâtiônàlizætiøn.com?ô=nëø' => '<a href="http://Iñtërnâtiônàlizætiøn.com?ô=nëø" target="_blank">http://Iñtërnâtiônàlizætiøn.com?ô=nëø</a>', 'www.Iñtërnâtiônàlizætiøn.com?ô=nëø' => '<a href="http://www.Iñtërnâtiônàlizætiøn.com?ô=nëø" target="_blank">www.Iñtërnâtiônàlizætiøn.com?ô=nëø</a>', 'Iñtërnâtiônàlizætiøn is important to http://moodle.org' => 'Iñtërnâtiônàlizætiøn is important to <a href="http://moodle.org" target="_blank">http://moodle.org</a>', 'moodle.org' => 'moodle.org', '<b>no link here</b>' => '<b>no link here</b>', '<b>a link here www.moodle.org</b>' => '<b>a link here <a href="http://www.moodle.org" target="_blank">www.moodle.org</a></b>', '<br />This is some text. www.moodle.com then some more text<br />' => '<br />This is some text. <a href="http://www.moodle.com" target="_blank">www.moodle.com</a> then some more text<br />', 'image<img src="http://moodle.org/logo/logo-240x60.gif" />' => 'image<img src="http://moodle.org/logo/logo-240x60.gif" />', 'image<img src="www.moodle.org/logo/logo-240x60.gif" />' => 'image<img src="www.moodle.org/logo/logo-240x60.gif" />', '<td background="http://moodle.org"> </td>' => '<td background="http://moodle.org"> </td>', '<td background="www.moodle.org"> </td>' => '<td background="www.moodle.org"> </td>', '<form name="input" action="http://moodle.org/submit.asp" method="get">' => '<form name="input" action="http://moodle.org/submit.asp" method="get">', 'partially escaped img tag <img src="http://moodle.org/logo/logo-240x60.gif" />' => 'partially escaped img tag <img src="http://moodle.org/logo/logo-240x60.gif" />', 'One more link like http://www.moodle.org to test' => 'One more link like <a href="http://www.moodle.org" target="_blank">http://www.moodle.org</a> to test', 'URL: http://127.0.0.1/one%28parenthesis%29/path?param=value' => 'URL: <a href="http://127.0.0.1/one%28parenthesis%29/path?param=value" target="_blank">http://127.0.0.1/one%28parenthesis%29/path?param=value</a>', 'URL: www.localhost.com/one%28parenthesis%29/path?param=value' => 'URL: <a href="http://www.localhost.com/one%28parenthesis%29/path?param=value" target="_blank">www.localhost.com/one%28parenthesis%29/path?param=value</a>', 'URL: http://127.0.0.1/path/to?param=value_with%28parenthesis%29¶m2=1' => 'URL: <a href="http://127.0.0.1/path/to?param=value_with%28parenthesis%29¶m2=1" target="_blank">http://127.0.0.1/path/to?param=value_with%28parenthesis%29¶m2=1</a>', 'URL: www.localhost.com/path/to?param=value_with%28parenthesis%29¶m2=1' => 'URL: <a href="http://www.localhost.com/path/to?param=value_with%28parenthesis%29¶m2=1" target="_blank">www.localhost.com/path/to?param=value_with%28parenthesis%29¶m2=1</a>'); foreach ($texts as $text => $correctresult) { $msg = "Testing text: " . str_replace('%', '%%', $text) . ": %s"; // Escape original '%' so sprintf() wont get confused convert_urls_into_links($text); $this->assertEqual($text, $correctresult, $msg); } //performance testing $reps = 1000; $time_start = microtime(true); for ($i = 0; $i < $reps; $i++) { $text = $this->get_test_text(); convert_urls_into_links($text); } $time_end = microtime(true); $new_time = $time_end - $time_start; $time_start = microtime(true); for ($i = 0; $i < $reps; $i++) { $text = $this->get_test_text(); $this->old_convert_urls_into_links($text); } $time_end = microtime(true); $old_time = $time_end - $time_start; $fast_enough = false; if ($new_time < $old_time) { $fast_enough = true; } $this->assertEqual($fast_enough, true, 'Timing test: ' . $new_time . 'secs (new) < ' . $old_time . 'secs (old)'); }
function test_convert_urls_into_links() { $string = "visit http://www.moodle.org"; convert_urls_into_links($string); $this->assertEqual($string, 'visit <a href="http://www.moodle.org">http://www.moodle.org</a>'); $string = "visit www.moodle.org"; convert_urls_into_links($string); $this->assertEqual($string, 'visit <a href="http://www.moodle.org">www.moodle.org</a>'); }
function test_convert_urls_into_links() { $texts = array('URL: http://moodle.org/s/i=1&j=2' => 'URL: <a href="http://moodle.org/s/i=1&j=2" target="_blank">http://moodle.org/s/i=1&j=2</a>', 'URL: www.moodle.org/s/i=1&j=2' => 'URL: <a href="http://www.moodle.org/s/i=1&j=2" target="_blank">www.moodle.org/s/i=1&j=2</a>', 'URL: https://moodle.org/s/i=1&j=2' => 'URL: <a href="https://moodle.org/s/i=1&j=2" target="_blank">https://moodle.org/s/i=1&j=2</a>', 'URL: http://moodle.org:8080/s/i=1' => 'URL: <a href="http://moodle.org:8080/s/i=1" target="_blank">http://moodle.org:8080/s/i=1</a>', 'http://moodle.org - URL' => '<a href="http://moodle.org" target="_blank">http://moodle.org</a> - URL', 'www.moodle.org - URL' => '<a href="http://www.moodle.org" target="_blank">www.moodle.org</a> - URL', '(http://moodle.org) - URL' => '(<a href="http://moodle.org" target="_blank">http://moodle.org</a>) - URL', '(www.moodle.org) - URL' => '(<a href="http://www.moodle.org" target="_blank">www.moodle.org</a>) - URL', '[http://moodle.org] - URL' => '[<a href="http://moodle.org" target="_blank">http://moodle.org</a>] - URL', '[www.moodle.org] - URL' => '[<a href="http://www.moodle.org" target="_blank">www.moodle.org</a>] - URL', '[http://moodle.org/main#anchor] - URL' => '[<a href="http://moodle.org/main#anchor" target="_blank">http://moodle.org/main#anchor</a>] - URL', '[www.moodle.org/main#anchor] - URL' => '[<a href="http://www.moodle.org/main#anchor" target="_blank">www.moodle.org/main#anchor</a>] - URL', 'URL: http://cc.org/url_(withpar)_go/?i=2' => 'URL: <a href="http://cc.org/url_(withpar)_go/?i=2" target="_blank">http://cc.org/url_(withpar)_go/?i=2</a>', 'URL: www.cc.org/url_(withpar)_go/?i=2' => 'URL: <a href="http://www.cc.org/url_(withpar)_go/?i=2" target="_blank">www.cc.org/url_(withpar)_go/?i=2</a>', 'URL: http://cc.org/url_(with)_(par)_go/?i=2' => 'URL: <a href="http://cc.org/url_(with)_(par)_go/?i=2" target="_blank">http://cc.org/url_(with)_(par)_go/?i=2</a>', 'URL: www.cc.org/url_(with)_(par)_go/?i=2' => 'URL: <a href="http://www.cc.org/url_(with)_(par)_go/?i=2" target="_blank">www.cc.org/url_(with)_(par)_go/?i=2</a>', 'URL: <a href="http://moodle.org">http://moodle.org</a>' => 'URL: <a href="http://moodle.org">http://moodle.org</a>', 'URL: <a href="http://moodle.org">www.moodle.org</a>' => 'URL: <a href="http://moodle.org">www.moodle.org</a>', 'URL: <a href="http://moodle.org"> http://moodle.org</a>' => 'URL: <a href="http://moodle.org"> http://moodle.org</a>', 'URL: <a href="http://moodle.org"> www.moodle.org</a>' => 'URL: <a href="http://moodle.org"> www.moodle.org</a>', 'URL: http://moodle.org/s/i=1&j=2.' => 'URL: <a href="http://moodle.org/s/i=1&j=2" target="_blank">http://moodle.org/s/i=1&j=2</a>.', 'URL: www.moodle.org/s/i=1&j=2.' => 'URL: <a href="http://www.moodle.org/s/i=1&j=2" target="_blank">www.moodle.org/s/i=1&j=2</a>.', 'URL: http://moodle.org)<br />' => 'URL: <a href="http://moodle.org" target="_blank">http://moodle.org</a>)<br />', 'URL: <p>text www.moodle.org</p> text' => 'URL: <p>text <a href="http://www.moodle.org" target="_blank">www.moodle.org</a></p> text', 'URL: www.moodle.org?u=1.23' => 'URL: <a href="http://www.moodle.org?u=1.23" target="_blank">www.moodle.org?u=1.23</a>', 'URL: www.moodle.org?u=test+param&' => 'URL: <a href="http://www.moodle.org?u=test+param&" target="_blank">www.moodle.org?u=test+param&</a>', 'URL: www.moodle.org?param=:)' => 'URL: <a href="http://www.moodle.org?param=:)" target="_blank">www.moodle.org?param=:)</a>', 'URL: http://moodle.org www.moodle.org' => 'URL: <a href="http://moodle.org" target="_blank">http://moodle.org</a> <a href="http://www.moodle.org" target="_blank">www.moodle.org</a>', 'URL: <a href="http://moodle.org">http://moodle.org</a> www.moodle.org <a class="customclass" href="http://moodle.org">http://moodle.org</a>' => 'URL: <a href="http://moodle.org">http://moodle.org</a> <a href="http://www.moodle.org" target="_blank">www.moodle.org</a> <a class="customclass" href="http://moodle.org">http://moodle.org</a>', 'http://subdomain.moodle.org - URL' => '<a href="http://subdomain.moodle.org" target="_blank">http://subdomain.moodle.org</a> - URL', 'http://subdomain.subdomain.moodle.org - URL' => '<a href="http://subdomain.subdomain.moodle.org" target="_blank">http://subdomain.subdomain.moodle.org</a> - URL', 'This contains http, http:// and www but no actual links.' => 'This contains http, http:// and www but no actual links.', 'This is a story about moodle.coming to a cinema near you.' => 'This is a story about moodle.coming to a cinema near you.', 'http://en.wikipedia.org/wiki/Slash_%28punctuation%29' => '<a href="http://en.wikipedia.org/wiki/Slash_%28punctuation%29" target="_blank">http://en.wikipedia.org/wiki/Slash_%28punctuation%29</a>', 'http://en.wikipedia.org/wiki/Slash_(punctuation)' => '<a href="http://en.wikipedia.org/wiki/Slash_(punctuation)" target="_blank">http://en.wikipedia.org/wiki/Slash_(punctuation)</a>', 'http://en.wikipedia.org/wiki/%28#Parentheses_.28_.29 - URL' => '<a href="http://en.wikipedia.org/wiki/%28#Parentheses_.28_.29" target="_blank">http://en.wikipedia.org/wiki/%28#Parentheses_.28_.29</a> - URL', 'http://en.wikipedia.org/wiki/(#Parentheses_.28_.29 - URL' => '<a href="http://en.wikipedia.org/wiki/(#Parentheses_.28_.29" target="_blank">http://en.wikipedia.org/wiki/(#Parentheses_.28_.29</a> - URL', 'http://Iñtërnâtiônàlizætiøn.com?ô=nëø' => '<a href="http://Iñtërnâtiônàlizætiøn.com?ô=nëø" target="_blank">http://Iñtërnâtiônàlizætiøn.com?ô=nëø</a>', 'www.Iñtërnâtiônàlizætiøn.com?ô=nëø' => '<a href="http://www.Iñtërnâtiônàlizætiøn.com?ô=nëø" target="_blank">www.Iñtërnâtiônàlizætiøn.com?ô=nëø</a>', 'moodle.org' => 'moodle.org'); foreach ($texts as $text => $correctresult) { if (mb_detect_encoding($text) == 'UTF-8') { $text_for_msg = utf8_decode($text); } else { $text_for_msg = $text; } //urldecode text or things like %28 cause sprintf's, looking for %s's, to throw an exception $msg = "Testing text: " . urldecode($text_for_msg) . ": %s"; convert_urls_into_links($text); //these decode's make all the strings non-garbled. The tests pass without them. if (mb_detect_encoding($text) == 'UTF-8') { $text = utf8_decode($text); } if (mb_detect_encoding($correctresult) == 'UTF-8') { $correctresult = utf8_decode($correctresult); } $this->assertEqual($text, $correctresult, $msg); } $reps = 1000; $time_start = microtime(true); for ($i = 0; $i < $reps; $i++) { $text = $this->get_test_text(); convert_urls_into_links($text); } $time_end = microtime(true); $new_time = $time_end - $time_start; $time_start = microtime(true); for ($i = 0; $i < $reps; $i++) { $text = $this->get_test_text(); $this->old_convert_urls_into_links($text); } $time_end = microtime(true); $old_time = $time_end - $time_start; $fast_enough = false; if ($new_time < $old_time) { $fast_enough = true; } $this->assertEqual($fast_enough, true, 'Timing test:'); }