Subversion Repositories PHPX

Rev

Blame | Last modification | View Log | RSS feed

1
/**
 * Converts the argument to a visible (X)HTML hyperlink
 * where its URI target is created from the argument.
 * Supported are e-mail addresses, domain names with
 * optional paths, and valid URIs.
 *
 * @param $text
 *   Argument to be converted.
 * @return
 *   The converted argument if it applies to a supported
 *   scheme, the unconverted argument otherwise.
 *  
 * @author (C) 2001-04-04T02:03
 *   mark.young@vdhinc.com at http://php.net/manual/en/ref.strings.php
 *
 * Minor correction to my HTMLEncode function.
 *
 * @author 2002-08-29T09:00
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
 *
 * - Added target="_blank"
 * - Added support for ftp(s)-URIs: (ht|f)
 * - Added support for search strings (?...=...&...=...), either
 *   with or without HTML entities: \?=(&\w;|&)
 * - Removed enclosing nl2br call because of preformatted display
 *
 * @author 2003-12-30T14:18
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
 *
 * - Removed target="_blank".
 * - Added PHPdoc.
 *
 * @author 2004-01-12T12:45
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
 *
 * - Added support for #fragment_identifiers in URIs.
 * - Added support for bugs and comments.
 *
 * @author 2004-01-13T01:29
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
 *
 * - Added support for bug aliases.
 *
 * @author 2004-01-26T20:43
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
 *
 * - Do not convert URIs in A elements.
 * - Do not allow URIs with "&" before search-string.
 * - camelCased function identifier.  Only classes
 *   and constructors should start with uppercase.
 *
 * @author 2004-01-27T14:07
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
 *
 * - Allow to convert URIs preceded by ">" but not followed by "</a>".
 * - Allow ";" to be part of the search string
 *
 * @author 2004-01-29T14:10
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
 *
 * - Require valid domain name for "bar" on conversion of "foo@bar" and
 *   "www.bar".
 * - Be case-insensitive except of bug aliases
 * - Escaped "-" in character classes if not meant as range metacharacter.
 * - Corrected year.
 *
 * @author 2004-02-14T17:37
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
 *
 * - Accept only valid Internet domain names
 * - Accept "%" within path and query part (to escape ASCII characters)
 *
 * @author 2004-02-27T19:21
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
 *
 * - Allow unescaped ":" in URI components, since it apparently does not
 *   "conflict with the reserved purpose" (RFC 2396, section 2.2.; here:
 *   scheme component)  
 * - Allow slashes, dots and dashes in URI components
 * - Removed invalid [...(...|...)...]
 *
 * @author 2004-03-01T21:48
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
 *
 * - Allow IPv4 addresses
 *
 * @author 2004-03-08T02:20
 *   Thomas Lahn <PointedEars@selfhtml.de> at localhost
 *
 * - Allow "+" and "," in query part
 * - Optimized character classes
 */

function htmlEncode($text)
{
  $searcharray = array(
    "'(?i)([\-_\w\d.]+@[\-\w\d.]+\.[A-Z]{2,})'i",
    "'(?i)((?:(?!://).{3}|^.{0,2}))(www\.[\-\w\d.]+\.[A-Z]{2,}"
      . "|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'i",
    "'(?i)(?<!href=\")((ht|f)tps?:\/\/([\-\w\d.]+\.[A-Z]{2,}"
      . "|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[#\-_~\w\d\.\/%]+"
      . "(\?([%&+-;=\w])*)?(;[=\d\w])?)(?!</a>)'i",
    "'(((?i)bug)\s+#?([\dA-Z_]+)\s+((?i)comment|Kommentar)\s+#?(\d+))'",
    "'(((?i)bug)\s+#?([\dA-Z_]+))'",
    "'(((?i)comment|Kommentar)\s+#?(\d+))'"
  );

  $replacearray = array(
    "<a href=\"mailto:\\1\">\\1</a>",
    "\\1http://\\2",
    "<a href=\"\\1\">\\1</a>",
    "<a href=\"./?bug=\\3#c\\5\">\\1</a>",
    "<a href=\"./?bug=\\3#details\">\\1</a>",
    "<a href=\"#c\\3\">\\1</a>"
  );

  return preg_replace($searcharray, $replacearray, $text);
}