diff --git a/lib/simpletest/testweblib.php b/lib/simpletest/testweblib.php
index 0b08a44336a..1c5372b9cbd 100644
--- a/lib/simpletest/testweblib.php
+++ b/lib/simpletest/testweblib.php
@@ -72,5 +72,28 @@ class web_test extends UnitTestCase {
$this->assertEqual(replace_ampersands_not_followed_by_entity("This & that "), "This & that ");
$this->assertEqual(replace_ampersands_not_followed_by_entity("This   that "), "This   that ");
}
+
+ function test_strip_links() {
+ $this->assertEqual(strip_links('this is a link'), 'this is a link');
+ }
+
+ function test_wikify_links() {
+ $this->assertEqual(wikify_links('this is a link'), 'this is a link [ http://someaddress.com/query ]');
+ }
+
+ function test_fix_non_standard_entities() {
+ $this->assertEqual(fix_non_standard_entities('£ä'), '£ä');
+ $this->assertEqual(fix_non_standard_entities('£ä'), '£ä');
+ }
+
+ function test_convert_urls_into_links() {
+ $string = "visit http://www.moodle.org";
+ convert_urls_into_links($string);
+ $this->assertEqual($string, 'visit http://www.moodle.org');
+
+ $string = "visit www.moodle.org";
+ convert_urls_into_links($string);
+ $this->assertEqual($string, 'visit www.moodle.org');
+ }
}
?>
diff --git a/lib/weblib.php b/lib/weblib.php
index 8ccfed3a73d..bc878d96c17 100644
--- a/lib/weblib.php
+++ b/lib/weblib.php
@@ -1654,7 +1654,7 @@ function format_string($string, $striplinks=true, $courseid=NULL ) {
} else {
// Otherwise strip just links if that is required (default)
if ($striplinks) { //strip links in string
- $string = preg_replace('/(]+?>)(.+?)(<\/a>)/is','$2',$string);
+ $string = strip_links($string);
}
$string = clean_text($string);
}
@@ -1677,6 +1677,38 @@ function replace_ampersands_not_followed_by_entity($string) {
return preg_replace("/\&(?![a-zA-Z0-9#]{1,8};)/", "&", $string);
}
+/**
+ * Given a string, replaces all .* by .* and returns the string.
+ *
+ * @param string $string
+ * @return string
+ */
+function strip_links($string) {
+ return preg_replace('/(]+?>)(.+?)(<\/a>)/is','$2',$string);
+}
+
+/**
+ * This expression turns links into something nice in a text format. (Russell Jungwirth)
+ *
+ * @param string $string
+ * @return string
+ */
+function wikify_links($string) {
+ return preg_replace('~(]*>([^<]*))~i','$3 [ $2 ]', $string);
+}
+
+/**
+ * Replaces non-standard HTML entities
+ *
+ * @param string $string
+ * @return string
+ */
+function fix_non_standard_entities($string) {
+ $text = preg_replace('/([0-9]+)(;?)/', '$1;', $string);
+ $text = preg_replace('/([0-9a-fA-F]+)(;?)/', '$1;', $text);
+ return $text;
+}
+
/**
* Given text in a variety of format codings, this function returns
* the text as plain text suitable for plain email.
@@ -1701,9 +1733,7 @@ function format_text_email($text, $format) {
case FORMAT_WIKI:
$text = wiki_to_html($text);
- /// This expression turns links into something nice in a text format. (Russell Jungwirth)
- /// From: http://php.net/manual/en/function.eregi-replace.php and simplified
- $text = preg_replace('~(]*>([^<]*))~i','$3 [ $2 ]', $text);
+ $text = wikify_links($text);
return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES)));
break;
@@ -1714,7 +1744,7 @@ function format_text_email($text, $format) {
case FORMAT_MOODLE:
case FORMAT_MARKDOWN:
default:
- $text = preg_replace('~(]*>([^<]*))~i','$3 [ $2 ]', $text);
+ $text = wikify_links($text);
return strtr(strip_tags($text), array_flip(get_html_translation_table(HTML_ENTITIES)));
break;
}
@@ -1859,8 +1889,7 @@ function clean_text($text, $format=FORMAT_MOODLE) {
$text = purify_html($text);
} else {
/// Fix non standard entity notations
- $text = preg_replace('/([0-9]+)(;?)/', "\\1;", $text);
- $text = preg_replace('/([0-9a-fA-F]+)(;?)/', "\\1;", $text);
+ $text = fix_non_standard_entities($text);
/// Remove tags that are not allowed
$text = strip_tags($text, $ALLOWED_TAGS);
@@ -2186,11 +2215,11 @@ function html_to_text($html) {
function convert_urls_into_links(&$text) {
/// Make lone URLs into links. eg http://moodle.com/
$text = preg_replace("~([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])~i",
- "\\1\\2://\\3\\4", $text);
+ '$1$2://$3$4', $text);
/// eg www.moodle.com
$text = preg_replace("~([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])~i",
- "\\1www.\\2\\3", $text);
+ '$1www.$2$3', $text);
}
/**