4 +-----------------------------------------------------------------------+
5 | program/include/rcube_string_replacer.php |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2009, RoundCube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Handle string replacements based on preg_replace_callback |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
24 * Helper class for string replacements based on preg_replace_callback
28 class rcube_string_replacer
30 public static $pattern = '/##str_replacement\[([0-9]+)\]##/';
32 private $values = array();
36 * Add a string to the internal list
38 * @param string String value
39 * @return int Index of value for retrieval
41 public function add($str)
43 $i = count($this->values);
44 $this->values[$i] = $str;
49 * Build replacement string
51 public function get_replacement($i)
53 return '##str_replacement['.$i.']##';
57 * Callback function used to build HTML links around URL strings
59 * @param array Matches result from preg_replace_callback
60 * @return int Index of saved string value
62 public function link_callback($matches)
65 $scheme = strtolower($matches[1]);
67 if ($scheme == 'http' || $scheme == 'https' || $scheme == 'ftp') {
68 $url = $matches[1] . '://' . $matches[2];
69 $i = $this->add(html::a(array('href' => $url, 'target' => "_blank"), Q($url)));
71 else if ($matches[2] == 'www.') {
72 $url = $matches[2] . $matches[3];
73 $i = $this->add($matches[1] . html::a(array('href' => 'http://' . $url, 'target' => "_blank"), Q($url)));
76 return $i >= 0 ? $this->get_replacement($i) : '';
80 * Callback function used to build mailto: links around e-mail strings
82 * @param array Matches result from preg_replace_callback
83 * @return int Index of saved string value
85 public function mailto_callback($matches)
87 $i = $this->add(html::a(array(
88 'href' => 'mailto:' . $matches[1],
89 'onclick' => "return ".JS_OBJECT_NAME.".command('compose','".JQ($matches[1])."',this)",
93 return $i >= 0 ? $this->get_replacement($i) : '';
97 * Look up the index from the preg_replace matches array
98 * and return the substitution value.
100 * @param array Matches result from preg_replace_callback
101 * @return string Value at index $matches[1]
103 public function replace_callback($matches)
105 return $this->values[$matches[1]];
109 * Replace substituted strings with original values
111 public function resolve($str)
113 return preg_replace_callback(self::$pattern, array($this, 'replace_callback'), $str);