Implement %AutoFormat.RemoveEmpty.RemoveNbsp, by popular demand.
[htmlpurifier/darkodev.git] / library / HTMLPurifier / Injector / Linkify.php
blob296dac282996255c811ab9908c9ed6f512ab924d
1 <?php
3 /**
4 * Injector that converts http, https and ftp text URLs to actual links.
5 */
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
19 return;
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);
26 $token = array();
28 // $i = index
29 // $c = count
30 // $l = is link
31 for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++, $l = !$l) {
32 if (!$l) {
33 if ($bits[$i] === '') continue;
34 $token[] = new HTMLPurifier_Token_Text($bits[$i]);
35 } else {
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');
46 // vim: et sw=4 sts=4