/** * Modifies the auto_link helper (url_helper) by accepting as an optional third * argument an array of html attributes for the anchor tags (just like the anchor helper). * * This array is supplied as the third argument, replacing the * optional argument $pop in the original helper. * * This modified helper attempts to be backward compatible with the use of the * original helper by accepting TRUE and FALSE as possible values for the $attributes * argument, and giving output identical to the original usage of the helper. * * use: auto_link($string, 'url' , array('class' => 'external', 'target'=>'_blank')); * use: auto_link($string, 'email', array('class' => 'email_link' , 'style' => 'color:red;')); * use(legacy): auto_link($string, 'url' , TRUE); * * @link https://github.com/EllisLab/CodeIgniter/wiki/auto-link * @author Derek Jones (original author) * @author Ivan Tcholakov (adaptation) * * @see url_helper * @link http://codeigniter.com/user_guide/helpers/url_helper.html * @param string $str * @param string $type * @param mixed $attributes * @return string */ function auto_link($str, $type = 'both', $attributes = '') { static $html_helper_loaded = null; if ($html_helper_loaded !== true) { get_instance()->load->helper('html'); $html_helper_loaded = true; } // MAKE THE THIRD ARGUMENT BACKWARD COMPATIBLE // here we deal with the original third argument $pop // which could be TRUE or FALSE, and was FALSE by default. if ($attributes === TRUE) { $attributes = ' target="_blank"'; } elseif ($attributes === FALSE) { $attributes = ''; } if ($attributes != '') { $attributes = ' ' . get_attributes_string($attributes); } // Find and replace any URLs. // Modified by Ivan Tcholakov, 19-DEC-2013. //if ($type !== 'email' && preg_match_all('#(\w*://|www\.)[^\s()<>;]+\w#i', $str, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) if ($type !== 'email' && preg_match_all('#(\\w*://|www\\.)[^\\s()<>;]+(\\w|/)#i', $str, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) { // We process the links in reverse order (last -> first) so that // the returned string offsets from preg_match_all() are not // moved as we add more HTML. foreach (array_reverse($matches) as $match) { // $match[0] is the matched string/link // $match[1] is either a protocol prefix or 'www.' // // With PREG_OFFSET_CAPTURE, both of the above is an array, // where the actual value is held in [0] and its offset at the [1] index. $a = '<a href="' . (strpos($match[1][0], '/') ? '' : 'http://') . $match[0][0] . '"' . $attributes . '>' . $match[0][0] . '</a>'; $str = substr_replace($str, $a, $match[0][1], strlen($match[0][0])); } } // Find and replace any emails. if ($type !== 'url' && preg_match_all('#([\\w\\.\\-\\+]+@[a-z0-9\\-]+\\.[a-z0-9\\-\\.]+[^[:punct:]\\s])#i', $str, $matches, PREG_OFFSET_CAPTURE)) { foreach (array_reverse($matches[0]) as $match) { if (filter_var($match[0], FILTER_VALIDATE_EMAIL) !== FALSE) { $str = substr_replace($str, safe_mailto($match[0], '', $attributes), $match[1], strlen($match[0])); } } } return $str; }
public function set_attributes($tag = 'body', $attributes) { // $attributes: string or array $tag = strtolower($tag); if (!in_array($tag, array('body', 'html'))) { return; } $this->_ci->load->_ci_cached_vars['template_' . $tag . '_tag_attributes'] = get_attributes_string($attributes); }
/** * Sets the value of the attribute * * @param string|array $attributes Array of attributes or HTML attribute string * @param string $name Attribute name * @param string $value Attribute value (will be set to $name if omitted) * * @return string A string containing result attributes */ function set_attribute($attributes, $name, $value = null) { $name = strtolower($name); $attributes = prepare_attributes($attributes); if (is_null($value)) { $value = $name; } $attributes[$name] = (string) $value; if ($name == 'class') { if ($attributes[$name] == '') { return remove_attribute($value, $name); } } return get_attributes_string($attributes); }