Subversion Repositories PHPX

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 PointedEar 1
/**
2
 * Converts the argument to a visible (X)HTML hyperlink
3
 * where its URI target is created from the argument.
4
 * Supported are e-mail addresses, domain names with
5
 * optional paths, and valid URIs.
6
 *
7
 * @param $text
8
 *   Argument to be converted.
9
 * @return
10
 *   The converted argument if it applies to a supported
11
 *   scheme, the unconverted argument otherwise.
12
 *  
13
 * @author (C) 2001-04-04T02:03
14
 *   mark.young@vdhinc.com at http://php.net/manual/en/ref.strings.php
15
 *
16
 * Minor correction to my HTMLEncode function.
17
 *
18
 * @author 2002-08-29T09:00
19
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
20
 *
21
 * - Added target="_blank"
22
 * - Added support for ftp(s)-URIs: (ht|f)
23
 * - Added support for search strings (?...=...&...=...), either
24
 *   with or without HTML entities: \?=(&\w;|&)
25
 * - Removed enclosing nl2br call because of preformatted display
26
 *
27
 * @author 2003-12-30T14:18
28
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
29
 *
30
 * - Removed target="_blank".
31
 * - Added PHPdoc.
32
 *
33
 * @author 2004-01-12T12:45
34
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
35
 *
36
 * - Added support for #fragment_identifiers in URIs.
37
 * - Added support for bugs and comments.
38
 *
39
 * @author 2004-01-13T01:29
40
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
41
 *
42
 * - Added support for bug aliases.
43
 *
44
 * @author 2004-01-26T20:43
45
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
46
 *
47
 * - Do not convert URIs in A elements.
48
 * - Do not allow URIs with "&" before search-string.
49
 * - camelCased function identifier.  Only classes
50
 *   and constructors should start with uppercase.
51
 *
52
 * @author 2004-01-27T14:07
53
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
54
 *
55
 * - Allow to convert URIs preceded by ">" but not followed by "</a>".
56
 * - Allow ";" to be part of the search string
57
 *
58
 * @author 2004-01-29T14:10
59
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
60
 *
61
 * - Require valid domain name for "bar" on conversion of "foo@bar" and
62
 *   "www.bar".
63
 * - Be case-insensitive except of bug aliases
64
 * - Escaped "-" in character classes if not meant as range metacharacter.
65
 * - Corrected year.
66
 *
67
 * @author 2004-02-14T17:37
68
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
69
 *
70
 * - Accept only valid Internet domain names
71
 * - Accept "%" within path and query part (to escape ASCII characters)
72
 *
73
 * @author 2004-02-27T19:21
74
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
75
 *
76
 * - Allow unescaped ":" in URI components, since it apparently does not
77
 *   "conflict with the reserved purpose" (RFC 2396, section 2.2.; here:
78
 *   scheme component)  
79
 * - Allow slashes, dots and dashes in URI components
80
 * - Removed invalid [...(...|...)...]
81
 *
82
 * @author 2004-03-01T21:48
83
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
84
 *
85
 * - Allow IPv4 addresses
86
 *
87
 * @author 2004-03-08T02:20
88
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
89
 *
90
 * - Allow "+" and "," in query part
91
 * - Optimized character classes
92
 */
93
function htmlEncode($text)
94
{
95
  $searcharray = array(
96
    "'(?i)([\-_\w\d.]+@[\-\w\d.]+\.[A-Z]{2,})'i",
97
    "'(?i)((?:(?!://).{3}|^.{0,2}))(www\.[\-\w\d.]+\.[A-Z]{2,}"
98
      . "|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'i",
99
    "'(?i)(?<!href=\")((ht|f)tps?:\/\/([\-\w\d.]+\.[A-Z]{2,}"
100
      . "|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[#\-_~\w\d\.\/%]+"
101
      . "(\?([%&+-;=\w])*)?(;[=\d\w])?)(?!</a>)'i",
102
    "'(((?i)bug)\s+#?([\dA-Z_]+)\s+((?i)comment|Kommentar)\s+#?(\d+))'",
103
    "'(((?i)bug)\s+#?([\dA-Z_]+))'",
104
    "'(((?i)comment|Kommentar)\s+#?(\d+))'"
105
  );
106
 
107
  $replacearray = array(
108
    "<a href=\"mailto:\\1\">\\1</a>",
109
    "\\1http://\\2",
110
    "<a href=\"\\1\">\\1</a>",
111
    "<a href=\"./?bug=\\3#c\\5\">\\1</a>",
112
    "<a href=\"./?bug=\\3#details\">\\1</a>",
113
    "<a href=\"#c\\3\">\\1</a>"
114
  );
115
 
116
  return preg_replace($searcharray, $replacearray, $text);
117
}