HTMLPurifier 4.4.0
|
00001 <?php 00002 00006 class HTMLPurifier_Injector_Linkify extends HTMLPurifier_Injector 00007 { 00008 00009 public $name = 'Linkify'; 00010 public $needed = array('a' => array('href')); 00011 00012 public function handleText(&$token) { 00013 if (!$this->allowsElement('a')) return; 00014 00015 if (strpos($token->data, '://') === false) { 00016 // our really quick heuristic failed, abort 00017 // this may not work so well if we want to match things like 00018 // "google.com", but then again, most people don't 00019 return; 00020 } 00021 00022 // there is/are URL(s). Let's split the string: 00023 // Note: this regex is extremely permissive 00024 $bits = preg_split('#((?:https?|ftp)://[^\s\'"<>()]+)#S', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE); 00025 00026 $token = array(); 00027 00028 // $i = index 00029 // $c = count 00030 // $l = is link 00031 for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++, $l = !$l) { 00032 if (!$l) { 00033 if ($bits[$i] === '') continue; 00034 $token[] = new HTMLPurifier_Token_Text($bits[$i]); 00035 } else { 00036 $token[] = new HTMLPurifier_Token_Start('a', array('href' => $bits[$i])); 00037 $token[] = new HTMLPurifier_Token_Text($bits[$i]); 00038 $token[] = new HTMLPurifier_Token_End('a'); 00039 } 00040 } 00041 00042 } 00043 00044 } 00045 00046 // vim: et sw=4 sts=4