4 * Injector that converts http, https and ftp text URLs to actual links.
6 class HTMLPurifier_Injector_Linkify
extends HTMLPurifier_Injector
9 public $name = 'Linkify';
10 public $needed = array('a' => array('href'));
12 public function handleText(&$token) {
13 if (!$this->allowsElement('a')) return;
15 if (strpos($token->data
, '://') === false) {
16 // our really quick heuristic failed, abort
17 // this may not work so well if we want to match things like
18 // "google.com", but then again, most people don't
22 // there is/are URL(s). Let's split the string:
23 // Note: this regex is extremely permissive
24 $bits = preg_split('#((?:https?|ftp)://[^\s\'"<>()]+)#S', $token->data
, -1, PREG_SPLIT_DELIM_CAPTURE
);
31 for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++
, $l = !$l) {
33 if ($bits[$i] === '') continue;
34 $token[] = new HTMLPurifier_Token_Text($bits[$i]);
36 $token[] = new HTMLPurifier_Token_Start('a', array('href' => $bits[$i]));
37 $token[] = new HTMLPurifier_Token_Text($bits[$i]);
38 $token[] = new HTMLPurifier_Token_End('a');