4 * Injector that converts http, https and ftp text URLs to actual links.
6 class HTMLPurifier_Injector_Linkify extends HTMLPurifier_Injector
11 public $name = 'Linkify';
16 public $needed = array('a' => array('href'));
19 * @param HTMLPurifier_Token $token
21 public function handleText(&$token)
23 if (!$this->allowsElement('a')) {
27 if (strpos($token->data, '://') === false) {
28 // our really quick heuristic failed, abort
29 // this may not work so well if we want to match things like
30 // "domainname.com", but then again, most people don't
34 // there is/are URL(s). Let's split the string:
35 // Note: this regex is extremely permissive
36 $bits = preg_split('#((?:https?|ftp)://[^\s\'",<>()]+)#Su', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE);
44 for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++, $l = !$l) {
46 if ($bits[$i] === '') {
49 $token[] = new HTMLPurifier_Token_Text($bits[$i]);
51 $token[] = new HTMLPurifier_Token_Start('a', array('href' => $bits[$i]));
52 $token[] = new HTMLPurifier_Token_Text($bits[$i]);
53 $token[] = new HTMLPurifier_Token_End('a');