/** * Inserts tag links into an HTML-formatted text. * * @param string $html * @param array $tags * @param array $options * @return string */ public static function autoTag($html, array $tags, array &$options = array()) { if (empty($tags)) { return $html; } $html = strval($html); $htmlNullified = utf8_strtolower($html); $htmlNullified = preg_replace_callback('#<a[^>]+>.+?</a>#', array(__CLASS__, '_autoTag_nullifyHtmlCallback'), $htmlNullified); $htmlNullified = preg_replace_callback('#<[^>]+>#', array(__CLASS__, '_autoTag_nullifyHtmlCallback'), $htmlNullified); // prepare the options $onceOnly = empty($options['onceOnly']) ? false : true; $options['autoTagged'] = array(); // reset this // sort tags with the longest one first // since 1.0.3 usort($tags, array(__CLASS__, '_autoTag_sortTagsByLength')); foreach ($tags as $tag) { $offset = 0; $tagText = utf8_strtolower($tag['tag']); $tagLength = utf8_strlen($tagText); while (true) { $pos = utf8_strpos($htmlNullified, $tagText, $offset); if ($pos !== false) { // the tag has been found if (self::_autoTag_hasValidCharacterAround($html, $pos, $tagText)) { // and it has good surrounding characters // start replacing $displayText = utf8_substr($html, $pos, $tagLength); $template = new XenForo_Template_Public('tinhte_xentag_bb_code_tag_tag'); $template->setParam('tag', $tag); $template->setParam('displayText', $displayText); $replacement = $template->render(); if (strlen($replacement) === 0) { // in case template system hasn't been initialized $replacement = sprintf('<a href="%s">%s</a>', XenForo_Link::buildPublicLink('tags', $tag), $displayText); } $html = utf8_substr_replace($html, $replacement, $pos, $tagLength); $htmlNullified = utf8_substr_replace($htmlNullified, str_repeat('_', utf8_strlen($replacement)), $pos, $tagLength); // sondh@2012-09-20 // keep track of the auto tagged tags $options['autoTagged'][$tagText][$pos] = $replacement; $offset = $pos + utf8_strlen($replacement); if ($onceOnly) { // auto link only once per tag // break the loop now break; // while (true) } } else { $offset = $pos + $tagLength; } } else { // no match has been found, stop working with this tag break; // while (true) } } } return $html; }
/** * UTF-8 aware alternative to strpos * Find position of first occurrence of a string * Note: This will get alot slower if offset is used * Note: requires utf8_strlen amd utf8_substr to be loaded * @param string haystack * @param string needle (you should validate this with utf8_is_valid) * @param integer offset in characters (from left) * @return mixed integer position or FALSE on failure * @see http://www.php.net/strpos * @see utf8_strlen * @see utf8_substr * @package utf8 * @subpackage strings */ function utf8_strpos($str, $needle, $offset = NULL) { if ( is_null($offset) ) { $ar = explode($needle, $str); if ( count($ar) > 1 ) { return utf8_strlen($ar[0]); } return FALSE; } else { if ( !is_int($offset) ) { trigger_error('utf8_strpos: Offset must be an integer',E_USER_ERROR); return FALSE; } $str = utf8_substr($str, $offset); if ( FALSE !== ( $pos = utf8_strpos($str, $needle) ) ) { return $pos + $offset; } return FALSE; } }
/** * @param string $internalUrl * @return mixed The URL to access the target file from outside, if available, or FALSE. */ public static function toExternalUrl($internalUrl) { $currentProc = ProcManager::getInstance()->getCurrentProcess(); if ($currentProc) { $checknum = $currentProc->getChecknum(); } else { $checknum = -1; } $urlParts = AdvancedPathLib::parse_url($internalUrl); if ($urlParts === false) { return $internalUrl; } if ($urlParts['scheme'] === EyeosAbstractVirtualFile::URL_SCHEME_SYSTEM) { // EXTERN try { $externPath = AdvancedPathLib::resolvePath($urlParts['path'], '/extern', AdvancedPathLib::OS_UNIX | AdvancedPathLib::RESOLVEPATH_RETURN_REFDIR_RELATIVE); return 'index.php?extern=' . $externPath; } catch (Exception $e) { } // APPS try { $appPath = AdvancedPathLib::resolvePath($urlParts['path'], '/apps', AdvancedPathLib::OS_UNIX | AdvancedPathLib::RESOLVEPATH_RETURN_REFDIR_RELATIVE); $appName = utf8_substr($appPath, 1, utf8_strpos($appPath, '/', 1)); $appFile = utf8_substr($appPath, utf8_strlen($appName) + 1); return 'index.php?checknum=' . $checknum . '&appName=' . $appName . '&appFile=' . $appFile; } catch (Exception $e) { } return $internalUrl; } //TODO return $internalUrl; }
function utf8_strpos($string, $needle, $offset = NULL) { if (is_null($offset)) { $data = explode($needle, $string, 2); if (count($data) > 1) { return utf8_strlen($data[0]); } return false; } else { if (!is_int($offset)) { trigger_error('utf8_strpos: Offset must be an integer', E_USER_ERROR); return false; } $string = utf8_substr($string, $offset); if (false !== ($position = utf8_strpos($string, $needle))) { return $position + $offset; } return false; } }
public static function update($targetClass, $targetPath, $sourceClass, $sourcesContents) { $targetContents = str_replace($sourceClass, $targetClass, $sourcesContents); $php = '<?php'; $pos = utf8_strpos($targetContents, $php); if ($pos !== false) { $replacement = sprintf("%s\n\n// updated by %s at %s", $php, __CLASS__, date('c')); $targetContents = utf8_substr_replace($targetContents, $replacement, $pos, utf8_strlen($php)); } $classPrefix = substr($targetClass, 0, strpos($targetClass, 'ShippableHelper_')); $offset = 0; while (true) { if (!preg_match('#DevHelper_Helper_ShippableHelper_[a-zA-Z_]+#', $targetContents, $matches, PREG_OFFSET_CAPTURE, $offset)) { break; } $siblingSourceClass = $matches[0][0]; $offset = $matches[0][1]; $siblingTargetClass = str_replace('DevHelper_Helper_', $classPrefix, $siblingSourceClass); $targetContents = substr_replace($targetContents, $siblingTargetClass, $offset, strlen($siblingSourceClass)); class_exists($siblingTargetClass); $offset += 1; } $targetContents = preg_replace('#\\* @version \\d+\\s*\\n#', '$0 * @see ' . $sourceClass . "\n", $targetContents, -1, $count); return DevHelper_Generator_File::filePutContents($targetPath, $targetContents); }
function render_attributes() { $this->_process_attributes(); if (!$this->path) { $action_path = $_SERVER['PHP_SELF']; $request = request::instance(); if ($node_id = $request->get_attribute('node_id')) { $action_path .= '?node_id=' . $node_id; } } else { $action_path = $this->path; } if (utf8_strpos($action_path, '?') === false) { $action_path .= '?'; } else { $action_path .= '&'; } if ($this->action) { $action_path .= 'action=' . $this->action; } if ((bool) $this->reload_parent) { $action_path .= '&reload_parent=1'; } $this->attributes['onclick'] = $this->onclick; $this->attributes['onclick'] .= "submit_form(this.form, '{$action_path}')"; parent::render_attributes(); unset($this->attributes['onclick']); }
function render_attributes() { if (!isset($this->attributes['path']) || !$this->attributes['path']) { $action_path = $_SERVER['PHP_SELF']; $request = request::instance(); if ($node_id = $request->get_attribute('node_id')) { $action_path .= '?node_id=' . $node_id; } } else { $action_path = $this->attributes['path']; } if (utf8_strpos($action_path, '?') === false) { $action_path .= '?'; } else { $action_path .= '&'; } if (isset($this->attributes['action'])) { $action_path .= 'action=' . $this->attributes['action']; } if (isset($this->attributes['reload_parent']) && $this->attributes['reload_parent']) { $action_path .= '&reload_parent=1'; unset($this->attributes['reload_parent']); } if (!isset($this->attributes['onclick'])) { $this->attributes['onclick'] = ''; } $this->attributes['onclick'] .= "submit_form(this.form, '{$action_path}')"; unset($this->attributes['path']); unset($this->attributes['action']); parent::render_attributes(); }
/** * Unicode: Returns the position of the first occurrence of needle in haystack * * @param string haystack * @param string needle * @param int offset * @return integer or false on failure */ function zula_strpos($haystack, $needle, $offset = null) { if (UNICODE_MBSTRING === true) { return $offset === null ? mb_strpos($haystack, $needle) : mb_strpos($haystack, $needle, $offset); } else { return $offset === null ? utf8_strpos($haystack, $needle) : utf8_strpos($haystack, $needle, $offset); } }
function _replace_callback($matches) { if (utf8_strpos($matches[3], '?') === false) { $matches[3] .= '?'; } $matches[3] .= '&' . $this->attributes_string; return $matches[1] . $matches[2] . $matches[3] . $matches[4]; }
function strpos_utf($haystack, $needle, $offset = 0) { if (function_exists('utf8_strpos')) { return utf8_strpos($haystack, $needle, $offset, 'UTF-8'); } else { return strpos($haystack, $needle, $offset); } }
function get_matching_phrase() { $phrase = parent::get_matching_phrase(); if (utf8_strpos($this->uri, 'yandpage') !== false) { $phrase = convert_cyr_string(urldecode($phrase), 'k', 'w'); } return $phrase; }
/** * UTF-8 aware alternative to strpos * Find position of first occurrence of a string * * @static * @access public * @param $str - string String being examined * @param $search - string String being searced for * @param $offset - int Optional, specifies the position from which the search should be performed * @return mixed Number of characters before the first match or FALSE on failure * @see http://www.php.net/strpos */ public static function strpos($str, $search, $offset = FALSE) { if ($offset === FALSE) { return utf8_strpos($str, $search); } else { return utf8_strpos($str, $search, $offset); } }
function nel_banned_text($text, $file) { $cancer = array('samefag', ''); $total_cancer = count($cancer); for ($i = 0; $i < $total_cancer; ++$i) { if ($cancer[$i] !== '') { $test = utf8_strpos($text, $cancer[$i]); if ($test !== FALSE) { nel_derp(17, array('origin' => 'SNACKS', 'cancer' => $cancer[$i])); } } } }
/** * @param string $path * @param SimpleXMLElement $xmlConf * @return AbstractFile */ public static function getRealFile($path, $xmlParams = null, $params = null) { $moutpointDescriptors = MountpointsManager::getInstance()->getMountpointDescriptorsList($path); $mountedPath = null; $realPath = AdvancedPathLib::getCanonicalURL($path); foreach ($moutpointDescriptors as $moutpointDescriptor) { $mountpointPath = $moutpointDescriptor->getMountpointPath(); if (utf8_strpos($realPath, $mountpointPath) === 0) { $mountedPath = $moutpointDescriptor->getTargetPath(); $mountedPath .= '/' . utf8_substr($realPath, utf8_strlen($mountpointPath)); } } if ($mountedPath !== null) { return FSI::getFile($mountedPath, $params); } return null; }
public function rdate($param, $time = 0) { $this->language->load('record/blog'); if (intval($time) == 0) { $time = time(); } $MonthNames = array($this->language->get('text_january'), $this->language->get('text_february'), $this->language->get('text_march'), $this->language->get('text_april'), $this->language->get('text_may'), $this->language->get('text_june'), $this->language->get('text_july'), $this->language->get('text_august'), $this->language->get('text_september'), $this->language->get('text_october'), $this->language->get('text_november'), $this->language->get('text_december')); if (strpos($param, 'M') === false) { return date($param, $time); } else { $str_begin = date(utf8_substr($param, 0, utf8_strpos($param, 'M')), $time); $str_middle = $MonthNames[date('n', $time) - 1]; $str_end = date(utf8_substr($param, utf8_strpos($param, 'M') + 1, utf8_strlen($param)), $time); $str_date = $str_begin . $str_middle . $str_end; return $str_date; } }
/** * Parses a var file into a data structure. Used in conjunction with an * Importtag */ function parse_var_file($filename) { $result = array(); $raw_lines = file($filename); while (list(, $line) = each($raw_lines)) { $equal_pos = utf8_strpos($line, '='); if ($equal_pos === false) { $result[utf8_trim($line)] = null; } else { $key = utf8_trim(utf8_substr($line, 0, $equal_pos)); if (utf8_strlen($key) > 0) { $result[$key] = utf8_trim(utf8_substr($line, $equal_pos + 1)); } } } return $result; }
/** * @param mixed $user An AbstractEyeosUser object or a username */ public static function getEyeosUserDirectory($user) { if (!is_dir(USERS_PATH)) { mkdir(USERS_PATH, 0777, true); } $username = $user; if ($user instanceof AbstractEyeosUser) { $username = $user->getName(); } else { if (!is_string($user)) { throw new EyeInvalidArgumentException('$user must be an instance of AbstractEyeosUser or a string.'); } } if (utf8_strpos($username, '_') === 0) { throw new EyeInvalidArgumentException('System users cannot have directory.'); } return USERS_PATH . '/' . utf8_basename($username); }
function add_url_query_items($url, $items = array()) { $str_params = ''; $request = request::instance(); if (($node_id = $request->get_attribute('node_id')) && !isset($items['node_id'])) { $items['node_id'] = $node_id; } if (utf8_strpos($url, '?') === false) { $url .= '?'; } foreach ($items as $key => $val) { $url = preg_replace("/&*{$key}=[^&]*/u", '', $url); $str_params .= "&{$key}={$val}"; } $items = utf8_explode('#', $url); $url = $items[0]; $fragment = isset($items[1]) ? '#' . $items[1] : ''; return $url . $str_params . $fragment; }
/** * UTF-8 aware alternative to strpos * Find position of first occurrence of a string * Note: This will get alot slower if offset is used * Note: requires utf8_strlen amd utf8_substr to be loaded * @param string haystack * @param string needle (you should validate this with utf8_is_valid) * @param integer offset in characters (from left) * @return mixed integer position or FALSE on failure * @see http://www.php.net/strpos * @see utf8_strlen * @see utf8_substr * @package utf8 * @subpackage strings */ function utf8_strpos($str, $needle, $offset = false) { if ($offset === false) { $ar = explode($needle, $str, 2); if (count($ar) > 1) { return utf8_strlen($ar[0]); } return false; } else { if (!is_int($offset)) { trigger_error('utf8_strpos: Offset must be an integer', E_USER_ERROR); return false; } $str = utf8_substr($str, $offset); if (($pos = utf8_strpos($str, $needle)) !== false) { return $pos + $offset; } return false; } }
/** * Inserts or replaces into the index. * * @see XenForo_Search_SourceHandler_Abstract::insertIntoIndex() */ public function insertIntoIndex($contentType, $contentId, $title, $message, $itemDate, $userId, $discussionId = 0, array $metadata = array()) { if (utf8_strlen($title) > 250) { $originalTitle = $title; $offset = 250; $title = utf8_substr($title, 0, $offset); if ($pos = utf8_strpos($title, ' ', $offset - 15)) { $title = utf8_substr($title, 0, $pos); $offset = $pos; } $message .= ' ' . utf8_substr($originalTitle, $offset); $title = trim($title); $message = trim($message); } $metadataPieces = array($this->getMetadataKey('user', $userId), $this->getMetadataKey('content', $contentType)); foreach ($metadata as $metadataKey => $value) { $piece = $this->getMetadataKey($metadataKey, $value); if (is_array($piece)) { $metadataPieces = array_merge($metadataPieces, $piece); } else { $metadataPieces[] = $piece; } } $db = $this->_getDb(); $row = '(' . $db->quote($contentType) . ', ' . $db->quote(intval($contentId)) . ', ' . $db->quote($title) . ', ' . $db->quote($message) . ', ' . $db->quote(implode(' ', $metadataPieces)) . ', ' . $db->quote(intval($itemDate)) . ', ' . $db->quote(intval($userId)) . ', ' . $db->quote(intval($discussionId)) . ')'; if ($this->_isRebuild) { $this->_bulkInserts[] = $row; $this->_bulkInsertLength += strlen($row); if ($this->_bulkInsertLength > 500000) { $this->_pushToIndex($this->_bulkInserts); $this->_bulkInserts = array(); $this->_bulkInsertLength = 0; } } else { $this->_pushToIndex($row); } }
/** * fulltext search helper * searches a text file with a given regular expression * no ACL checks are performed. This have to be done by * the caller if necessary. * * @param array $data reference to array for results * @param string $base base directory * @param string $file file name to search in * @param string $reg regular expression to search for * @param array $words words that should be marked in the results * * @author Andreas Gohr <*****@*****.**> * @author Matthias Grimm <*****@*****.**> * * @deprecated - fulltext indexer is used instead */ function search_regex(&$data, $base, $file, $reg, $words) { //get text $text = io_readfile($base . '/' . $file); //lowercase text (u modifier does not help with case) $lctext = utf8_strtolower($text); //do the fulltext search $matches = array(); if ($cnt = preg_match_all('#' . $reg . '#usi', $lctext, $matches)) { //this is not the best way for snippet generation but the fastest I could find $q = $words[0]; //use first word for snippet creation $p = utf8_strpos($lctext, $q); $f = $p - 100; $l = utf8_strlen($q) + 200; if ($f < 0) { $f = 0; } $snippet = '<span class="search_sep"> ... </span>' . htmlspecialchars(utf8_substr($text, $f, $l)) . '<span class="search_sep"> ... </span>'; $mark = '(' . join('|', $words) . ')'; $snippet = preg_replace('#' . $mark . '#si', '<strong class="search_hit">\\1</strong>', $snippet); $data[] = array('id' => pathID($file), 'count' => preg_match_all('#' . $mark . '#usi', $lctext, $matches), 'poswords' => join(' ', $words), 'snippet' => $snippet); } return true; }
/** * Unicode (UTF-8) analogue of standard @link http://php.net/strpos strpos PHP function. * Find the position of the first occurrence of a case-sensitive UTF-8 encoded string. * Returns the numeric position (offset in amount of UTF-8 characters) * of the first occurrence of needle in the haystack string. * * @param string $haystack The UTF-8 encoded string being searched in. * @param integer $needle The UTF-8 encoded string being searched for. * @param integer $offset [optional] The optional offset parameter allows you to specify which character in haystack to start searching. * The position returned is still relative to the beginning of haystack. * @return integer|boolean Returns the position as an integer. If needle is not found, the function will return boolean FALSE. */ public function ustrpos($haystack, $needle, $offset = 0) { switch ($this->utfAction) { case 0: return strpos($haystack, $needle, $offset); case 1: return mb_strpos($haystack, $needle, $offset); } return utf8_strpos($haystack, $needle, $offset); }
/** * rss_yahoo_notify($url) will handle yahoo notification of new content * @access private */ function rss_yahoo_notify($url = '') { global $user, $config, $phpbb_seo; $url = !empty($url) ? str_replace('&', '&', $url) : (!empty($this->url_config['rss_yahoo_notify_url']) ? $this->url_config['rss_yahoo_notify_url'] : ''); $url = trim($url); if (empty($url) || !$this->rss_config['rss_yahoo_notify'] || empty($this->rss_config['yahoo_appid'])) { return; } // No more than 200 pings a day! if (@$config['gym_pinged_today'] > 200) { // @TODO add logs about this ? return; } $skip = array('://localhost', '://127.0.0.1', '://192.168.', '://169.254.'); foreach ($skip as $_skip) { if (utf8_strpos($url, $_skip) !== false) { // @TODO add logs about this ? return; } } // If ssl is not forced, always ping with http urls $url = $phpbb_seo->sslify($url, $phpbb_seo->ssl['forced']); $not_curl = true; $timout = 3; // The Yahoo! Web Services request // Based on the Yahoo! developper hints : http://developer.yahoo.com/php/ $request = "http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=" . urlencode($this->rss_config['yahoo_appid']) . '&url=' . urlencode($url); if (function_exists('curl_exec')) { $not_curl = false; // Initialize the session $session = curl_init($request); // Set curl options curl_setopt($session, CURLOPT_HEADER, false); curl_setopt($session, CURLOPT_USERAGENT, 'GYM Sitemaps & RSS / www.phpBB-SEO.com'); curl_setopt($session, CURLOPT_TIMEOUT, $timout); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Make the request $response = curl_exec($session); // Close the curl session curl_close($session); // Get HTTP Status code from the response $status_codes = array(); preg_match('/\\d\\d\\d/', $response, $status_code); $status_code = $status_codes[0]; // Get the XML from the response, bypassing the header if (!($xml = strstr($response, '<?xml'))) { $xml = null; $not_curl = true; } } else { if ($not_curl && function_exists('file_get_contents')) { ini_set('user_agent', 'GYM Sitemaps & RSS / www.phpBB-SEO.com'); ini_set('default_socket_timeout', $timout); // Make the request if ($xml = file_get_contents($request)) { // Retrieve HTTP status code list($version, $status_code, $msg) = explode(' ', $http_response_header[0], 3); } else { // $user->lang['RSS_YAHOO_NO_METHOD'] = sprintf($user->lang['RSS_YAHOO_NO_METHOD'], $request, $xml); $this->gym_error(503, 'RSS_YAHOO_NO_METHOD', __FILE__, __LINE__); } } } // Check the XML return message // Do it this way here in case curl actually returned no header // but did get the proper answer. if (!strpos($xml, 'success')) { // Check the HTTP Status code switch ($status_code) { case 200: // Success set_config('gym_pinged_today', @$config['gym_pinged_today'] + 1, 1); break; case 503: $this->gym_error(500, 'RSS_YAHOO_503', __FILE__, __LINE__); break; case 403: $this->gym_error(500, 'RSS_YAHOO_403', __FILE__, __LINE__); break; case 400: // $user->lang['RSS_YAHOO_400_MSG'] = sprintf($user->lang['RSS_YAHOO_400_MSG'], $request, $xml); $this->gym_error(500, 'RSS_YAHOO_400', __FILE__, __LINE__); break; default: // $user->lang['RSS_YAHOO_ERROR_MSG'] = sprintf($user->lang['RSS_YAHOO_400_MSG'], $status_code, $request, $xml); $this->gym_error(500, 'RSS_YAHOO_500', __FILE__, __LINE__); } } return; }
/** * @see http://ca.php.net/manual/en/function.strpos.php * @param $haystack string Input haystack to search * @param $needle string Input needle to search for * @param $offset int Offset at which to begin searching * @return int Position of needle within haystack */ static function strpos($haystack, $needle, $offset = 0) { if (defined('ENABLE_MBSTRING')) { require_once './lib/pkp/lib/phputf8/mbstring/core.php'; } else { require_once './lib/pkp/lib/phputf8/utils/unicode.php'; require_once './lib/pkp/lib/phputf8/native/core.php'; } return utf8_strpos($haystack, $needle, $offset); }
/** * Generates a text with approx. the specified length which contains the specified words and their context * * @param string $text The full text from which context shall be extracted * @param string $words An array of words which should be contained in the result, has to be a valid part of a PCRE pattern (escape with preg_quote!) * @param int $length The desired length of the resulting text, however the result might be shorter or longer than this value * * @return string Context of the specified words separated by "..." */ function get_context($text, $words, $length = 400) { // first replace all whitespaces with single spaces $text = preg_replace('/ +/', ' ', strtr($text, "\t\n\r\f ", ' ')); // we need to turn the entities back into their original form, to not cut the message in between them $entities = array('<', '>', '[', ']', '.', ':', ':'); $characters = array('<', '>', '[', ']', '.', ':', ':'); $text = str_replace($entities, $characters, $text); $word_indizes = array(); if (sizeof($words)) { $match = ''; // find the starting indizes of all words foreach ($words as $word) { if ($word) { if (preg_match('#(?:[^\\w]|^)(' . $word . ')(?:[^\\w]|$)#i', $text, $match)) { if (empty($match[1])) { continue; } $pos = utf8_strpos($text, $match[1]); if ($pos !== false) { $word_indizes[] = $pos; } } } } unset($match); if (sizeof($word_indizes)) { $word_indizes = array_unique($word_indizes); sort($word_indizes); $wordnum = sizeof($word_indizes); // number of characters on the right and left side of each word $sequence_length = (int) ($length / (2 * $wordnum)) - 2; $final_text = ''; $word = $j = 0; $final_text_index = -1; // cycle through every character in the original text for ($i = $word_indizes[$word], $n = utf8_strlen($text); $i < $n; $i++) { // if the current position is the start of one of the words then append $sequence_length characters to the final text if (isset($word_indizes[$word]) && $i == $word_indizes[$word]) { if ($final_text_index < $i - $sequence_length - 1) { $final_text .= '... ' . preg_replace('#^([^ ]*)#', '', utf8_substr($text, $i - $sequence_length, $sequence_length)); } else { // if the final text is already nearer to the current word than $sequence_length we only append the text // from its current index on and distribute the unused length to all other sequenes $sequence_length += (int) (($final_text_index - $i + $sequence_length + 1) / (2 * $wordnum)); $final_text .= utf8_substr($text, $final_text_index + 1, $i - $final_text_index - 1); } $final_text_index = $i - 1; // add the following characters to the final text (see below) $word++; $j = 1; } if ($j > 0) { // add the character to the final text and increment the sequence counter $final_text .= utf8_substr($text, $i, 1); $final_text_index++; $j++; // if this is a whitespace then check whether we are done with this sequence if (utf8_substr($text, $i, 1) == ' ') { // only check whether we have to exit the context generation completely if we haven't already reached the end anyway if ($i + 4 < $n) { if ($j > $sequence_length && $word >= $wordnum || utf8_strlen($final_text) > $length) { $final_text .= ' ...'; break; } } else { // make sure the text really reaches the end $j -= 4; } // stop context generation and wait for the next word if ($j > $sequence_length) { $j = 0; } } } } return str_replace($characters, $entities, $final_text); } } if (!sizeof($words) || !sizeof($word_indizes)) { return str_replace($characters, $entities, utf8_strlen($text) >= $length + 3 ? utf8_substr($text, 0, $length) . '...' : $text); } }
/** * Extracts an excerpt from the "$text" surrounding the "$phrase" with a * number of characters on each side determined by "$radius". If the phrase * isn't found, '' is returned. * * Example: * * <?=$text_helper->excerpt("hello my world", "my", 3);?> * //outputs: ...lo my wo... * */ function excerpt($text, $phrase, $radius = 100, $excerpt_string = '...') { if (empty($text)) { return $text; } $phrase = preg_quote($phrase); if (preg_match('/(' . $phrase . ')/iu', $text, $found)) { $found_pos = utf8_strpos($text, $found[0]); $start_pos = max($found_pos - $radius, 0); $end_pos = min($found_pos + utf8_strlen($phrase) + $radius, utf8_strlen($text)); $prefix = $start_pos > 0 ? $excerpt_string : ''; $postfix = $end_pos < utf8_strlen($text) ? $excerpt_string : ''; return $prefix . trim(utf8_substr($text, $start_pos, $end_pos - $start_pos)) . $postfix; } return ''; }
function mb_strpos($haystack, $needle, $offset = null) { if ($offset === null) { return utf8_strpos($haystack, $needle); } return utf8_strpos($haystack, $needle, $offset); }
/** * Parses a URL and return its components * * @see parse_url() in PHP manual * @param string $path The URL/path to parse * @param int $flags * OS_WINDOWS: Set it to force parsing assuming a Windows filesystem.<br /> * OS_UNIX: Set it to force parsing assuming a UNIX filesystem.<br /> * PARSE_URL_NO_AUTOSET_SCHEME: Set it to prevent autosetting scheme (file:// by default) in the returned components.<br /> * PARSE_URL_KEEPFRAGMENT: Set it to force keeping detected fragment even if the scheme may not support it.<br /> * PARSE_URL_FRAGMENT2PATH: Set it to force the fragment to be considered within the path component.<br /> * PARSE_URL_DONTRESOLVE: Set it to prevent resolving path (if file:// for example). * @return array */ public static function parse_url($path, $flags = self::NONE) { if ($flags & self::OS_WINDOWS) { $isWindows = true; } elseif ($flags & self::OS_UNIX) { $isWindows = false; } else { $isWindows = self::isCurrentOS(self::OS_WINDOWS); } if ($isWindows) { $path = self::unifyPath($path, true); } //=========== URL ANALYZE (begin) =========== //We first try with PHP standard function parse_url() but this one may fail on unknown schemes //so in this case we try regexp (slower) to break it down into parts. $errorParsing = false; try { $urlParts = parse_url($path); if ($urlParts === false) { $errorParsing = true; } } catch (EyeErrorException $e) { $errorParsing = true; } if ($errorParsing) { $errorParsing = false; // Reset flag /** @see http://www.ietf.org/rfc/rfc3986.txt */ //(extracts only scheme, "authority" (user-pass-host-port), path, query and fragment) if (preg_match('`^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?`', $path, $matches)) { $urlParts = array(); if (isset($matches[2])) { $urlParts['scheme'] = $matches[2]; } if (isset($matches[4]) && !empty($matches[4])) { //extract user, pass, host and port from authority if (preg_match('`^(([^:/?#]+)(:([^:/?#]+))?@)?([^:/?#@]+)(:([0-9]{1,5}))?$`', $matches[4], $matches2)) { if (isset($matches2[2]) && !empty($matches2[2])) { $urlParts['user'] = $matches2[2]; } if (isset($matches2[4]) && !empty($matches2[4])) { $urlParts['pass'] = $matches2[4]; } if (isset($matches2[5]) && !empty($matches2[5])) { $urlParts['host'] = $matches2[5]; } if (isset($matches2[7]) && !empty($matches2[7])) { $urlParts['port'] = $matches2[7]; } } else { $errorParsing = true; } } if (isset($matches[5]) && !empty($matches[5])) { $urlParts['path'] = $matches[5]; } else { $urlParts['path'] = '/'; } if (isset($matches[7]) && !empty($matches[7])) { $urlParts['query'] = $matches[7]; } if (isset($matches[9]) && !empty($matches[9])) { $urlParts['fragment'] = $matches[9]; } } else { $errorParsing = true; } } if ($errorParsing) { throw new InvalidArgumentException('Unable to parse given path/URL "' . $path . '"'); } //=========== URL ANALYZE (end) =========== $path = urlencode($path); // FIXME!!! if (!($flags & self::PARSE_URL_NO_AUTOSET_SCHEME) && !isset($urlParts['scheme'])) { $urlParts['scheme'] = 'file'; } //Windows platforms have the following special corrective behaviour if ($isWindows) { if (isset($urlParts['path'])) { $urlParts['path'] = self::unifyPath($urlParts['path']); } //misunderstood scheme? (Windows drive letter) if (isset($urlParts['scheme']) && preg_match('/^[a-z]{1}$/i', $urlParts['scheme'])) { //misunderstood path? if (isset($urlParts['host']) && $urlParts['host'] != 'localhost') { $urlParts['path'] = '/' . $urlParts['host'] . '/' . $urlParts['path']; unset($urlParts['host']); } $urlParts['path'] = strtoupper($urlParts['scheme']) . ':' . $urlParts['path']; //autoset scheme (file:// by default) if (!($flags & self::PARSE_URL_NO_AUTOSET_SCHEME)) { $urlParts['scheme'] = 'file'; } else { unset($urlParts['scheme']); } } //misunderstood host? (Windows drive letter) if (isset($urlParts['host']) && preg_match('/^[a-z]{1}$/i', $urlParts['host'])) { if (!isset($urlParts['path']) || preg_match('/^([a-z]{1}):/i', $urlParts['path']) === 0) { $urlParts['path'] = strtoupper($urlParts['host']) . ':' . @$urlParts['path']; unset($urlParts['host']); } //autoset scheme (file:// by default) if (!($flags & self::PARSE_URL_NO_AUTOSET_SCHEME)) { $urlParts['scheme'] = 'file'; } } //windows absolute path? if (isset($urlParts['path']) && preg_match('/^\\/([a-z]{1})(:\\/.*)$/i', $urlParts['path'], $matches)) { $urlParts['path'] = strtoupper($matches[1]) . $matches[2]; } } //autocomplete and unify result path if (isset($urlParts['path']) && $urlParts['path'] != '') { $urlParts['path'] = self::unifyPath($urlParts['path']); } else { $urlParts['path'] = '/'; } //root path under Windows? if ($isWindows && isset($urlParts['scheme']) && $urlParts['scheme'] == 'file' && utf8_strpos($urlParts['path'], '/') === 0 && !(utf8_strpos($urlParts['path'], ':') === 2)) { $urlParts['path'] = utf8_strtoupper(utf8_substr(realpath('.'), 0, 2)) . $urlParts['path']; } if (!($flags & self::PARSE_URL_DONTRESOLVE)) { //relative path? (to PHP's current dir) if (utf8_strpos($urlParts['path'], './') === 0) { //was: if (preg_match('/^\.\//', $urlParts['path'])) { $urlParts['path'] = self::realpath($urlParts['path'], true); } elseif (preg_match('/\\.{2}\\//', $urlParts['path'])) { $urlParts['path'] = self::realpath($urlParts['path'], $urlParts['scheme'] == 'file'); } } //no host provided? (file://) if (isset($urlParts['scheme']) && $urlParts['scheme'] == 'file' && isset($urlParts['host']) && $urlParts['host'] != 'localhost') { $urlParts['path'] = '/' . $urlParts['host'] . $urlParts['path']; unset($urlParts['host']); } //autocomplete and unify result path if (isset($urlParts['path']) && $urlParts['path'] != '') { $urlParts['path'] = self::getCanonicalPath($urlParts['path']); } else { $urlParts['path'] = '/'; } //scheme to lowercase if (isset($urlParts['scheme'])) { $urlParts['scheme'] = utf8_strtolower($urlParts['scheme']); } //consider fragment (scheme://path#fragment) inside or outside path? if ($flags & self::PARSE_URL_FRAGMENT2PATH || !($flags & self::PARSE_URL_KEEPFRAGMENT) && isset($urlParts['fragment']) && in_array($urlParts['scheme'], self::$ParseUrl_fragment2PathProtocols)) { $urlParts['path'] .= '#' . $urlParts['fragment']; unset($urlParts['fragment']); } // var_dump($urlParts); return $urlParts; }
public function filterString($string, array $rendererStates) { $isString = true; if (is_array($string)) { // array is our way of marking tag originals prepend/append text $isString = false; $string = reset($string); } if ($isString) { // checks if the string is an URI of some kind $isString = !Zend_Uri::check($string); } if ($isString) { // checks if the parent tag has some strange requirements $tagDataStack = $rendererStates['tagDataStack']; $lastTagDataStack = array_pop($tagDataStack); if ($lastTagDataStack['tag'] === 'hashtag') { // well, this is a hashtag already... $isString = false; } else { $parentTagInfo = $this->_parent__getTagRule($lastTagDataStack['tag']); if (!empty($parentTagInfo['plainChildren'])) { // parent tag asks for plain children, we should do nothing $isString = false; } elseif (!empty($parentTagInfo['stopSmilies']) or !empty($parentTagInfo['stopLineBreakConversion'])) { // parent tag asks for some functionalities disabled, we should disable ourself // too $isString = false; } } } if ($isString) { $offset = 0; while (true) { $pos = utf8_strpos($string, '#', $offset); if ($pos === false) { break; } $offset = $pos + 1; if ($pos > 0) { $beforeTagText = utf8_substr($string, $pos - 1, 1); if (!preg_match(Tinhte_XenTag_Integration::REGEX_VALID_CHARACTER_AROUND, $beforeTagText)) { // the before character of tag text is not a valid character, dismiss the found // tag text continue; } } $stringForPregMatch = utf8_substr($string, $pos + 1); if (preg_match('/[^a-zA-Z0-9]/', $stringForPregMatch, $matches, PREG_OFFSET_CAPTURE)) { $nonTagTextPos = $matches[0][1]; } else { // get all of the remaining characters $nonTagTextPos = utf8_strlen($stringForPregMatch); } $nonTagTextPos += $pos + 1; $tagText = utf8_trim(utf8_substr($string, $pos + 1, $nonTagTextPos - 1 - $pos)); if (utf8_strlen($tagText) < Tinhte_XenTag_Option::get('tagMinLength')) { // too short continue; } $afterTagText = utf8_substr($string, $nonTagTextPos, 1); if (!empty($afterTagText)) { if (!preg_match(Tinhte_XenTag_Integration::REGEX_VALID_CHARACTER_AROUND, $afterTagText)) { // the after character of tag text is not a valid character, dismiss the found // tag text $tagText = ''; } } if (!empty($tagText)) { $this->_Tinhte_XenTag_autoHashtagTexts[Tinhte_XenTag_Helper::getSafeTagTextForSearch($tagText)] = $tagText; // add bb code wrapping $replacement = sprintf('[HASHTAG]#%s[/HASHTAG]', $tagText); $string = utf8_substr_replace($string, $replacement, $pos, $nonTagTextPos - $pos); $pos += utf8_strlen($replacement) - 1; } $offset = $pos + 1; } } return parent::filterString($string, $rendererStates); }
public function files() { $json = array(); if (!empty($this->request->post['directory'])) { $directory = DIR_IMAGE . 'data/' . str_replace('../', '', $this->request->post['directory']); } else { $directory = DIR_IMAGE . 'data/'; } $allowed = array('.jpg', '.jpeg', '.png', '.gif'); $files = glob(rtrim($directory, '/') . '/*'); if ($files) { foreach ($files as $file) { if (is_file($file)) { $ext = strrchr($file, '.'); } else { $ext = ''; } if (in_array(strtolower($ext), $allowed)) { $size = filesize($file); $i = 0; $suffix = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); while ($size / 1024 > 1) { $size = $size / 1024; $i++; } $json[] = array('filename' => basename($file), 'file' => utf8_substr($file, utf8_strlen(DIR_IMAGE . 'data/')), 'size' => round(utf8_substr($size, 0, utf8_strpos($size, '.') + 4), 2) . $suffix[$i]); } } } $this->response->setOutput(json_encode($json)); }