function str_replace_limit($haystack, $needle, $replace, $limit, $start_pos = 0) { if ($limit <= 0) { return $haystack; } else { $pos = strrpos($haystack, $needle, $start_pos); if ($pos !== false) { $newstring = substr_replace($haystack, $replace, $pos, strlen($needle)); return str_replace_limit($newstring, $needle, $replace, $limit - 1, $pos + strlen($replace)); } else { return $haystack; } } }
private static function check_required_params($params) { foreach ($params as $param => $validation) { if (is_int($param)) { $param = $validation; unset($validation); } if (is_int(strpos($param, '['))) { $param = '[' . str_replace_limit('[', '][', $param); } else { $param = "[{$param}]"; } $data = isset_array($param, Request::$params); if (!empty($validation)) { if (!validate_data($data, $validation)) { return false; } } elseif ($data === null) { return false; } } return true; }
/** * Parses all Callback tags, and sends them through the given $callback. * * @param string $text Text to parse * @param mixed $callback Callback to apply to each tag * @param bool $inConditional Whether we are in a conditional tag * @return string */ public function parseCallbackTags($text, $data, $callback) { $this->setupRegex(); $inCondition = $this->inCondition; if ($inCondition) { $regex = '/\\{\\s*(' . $this->variableRegex . ')(\\s+.*?)?\\s*\\}/ms'; } else { $regex = '/\\{\\{\\s*(' . $this->variableRegex . ')(\\s+.*?)?\\s*(\\/)?\\}\\}/ms'; } /** * $match[0][0] is the raw tag * $match[0][1] is the offset of raw tag * $match[1][0] is the callback name * $match[1][1] is the offset of callback name * $match[2][0] is the parameters * $match[2][1] is the offset of parameters * $match[3][0] is the self closure * $match[3][1] is the offset of closure */ while (preg_match($regex, $text, $match, PREG_OFFSET_CAPTURE)) { $selfClosed = false; $parameters = array(); $tag = $match[0][0]; $start = $match[0][1]; $name = $match[1][0]; if (isset($match[2])) { $cb_data = $data; if (!empty($this->parser_callback_data)) { $data = $this->toArray($data); $cb_data = array_merge($this->parser_callback_data, $data); } $raw_params = $this->injectExtractions($match[2][0], '__cond_str'); $parameters = $this->parseParameters($raw_params, $cb_data, $callback); } if (isset($match[3])) { $selfClosed = true; } $content = ''; $temp_text = substr($text, $start + strlen($tag)); if (preg_match('/\\{\\{\\s*\\/' . preg_quote($name, '/') . '\\s*\\}\\}/m', $temp_text, $match, PREG_OFFSET_CAPTURE) && !$selfClosed) { $content = substr($temp_text, 0, $match[0][1]); $tag .= $content . $match[0][0]; // Is there a nested block under this one existing with the same name? $nested_regex = '/\\{\\{\\s*(' . preg_quote($name, '/') . ')(\\s.*?)\\}\\}(.*?)\\{\\{\\s*\\/\\1\\s*\\}\\}/ms'; if (preg_match($nested_regex, $content . $match[0][0], $nested_matches)) { $nested_content = preg_replace('/\\{\\{\\s*\\/' . preg_quote($name, '/') . '\\s*\\}\\}/m', '', $nested_matches[0]); $content = $this->createExtraction('nested_looped_tags', $nested_content, $nested_content, $content); } } $replacement = call_user_func_array($callback, array($name, $parameters, $content)); // Added by Ivan Tcholakov, 23-DEC-2015. if ($this->is_attribute_being_parsed && !$inCondition) { $replacement = $this->serialize($replacement); } // $replacement = $this->parseRecursives($replacement, $content, $callback); if ($inCondition) { $replacement = $this->valueToLiteral($replacement); } else { $replacement = @(string) $replacement; } // // Modified by Ivan Tcholakov, 25-JAN2016. // Trying to avoid the error "preg_replace(): Compilation failed: regular expression is too large at offset ...". //$text = preg_replace('/'.preg_quote($tag, '/').'/m', addcslashes($replacement, '\\$'), $text, 1); $text = str_replace_limit($tag, $replacement, $text, $replacement_count, 1); // $text = $this->injectExtractions($text, 'nested_looped_tags'); } return $text; }
function show_keyword($content) { global $Key_word, $webdb, $pre, $db; if (!$webdb[ifShowKeyword]) { return $content; } require_once ROOT_PATH . "data/keyword.php"; //把图片描述去掉 //$content=preg_replace("/ alt=([^ >]+)/is","",$content); foreach ($Key_word as $key => $value) { if (!$value) { $value = "{$webdb['www_url']}/do/search.php?type=title&keyword=" . urlencode($key); } //$rs = $db->get_one("SELECT num FROM {$pre}keyword WHERE `keywords` = '{$key}'"); //$content=str_replace_limit($key,"<a href=$value style=text-decoration:underline;font-size:14px;color:{$webdb[ShowKeywordColor]}; target=_blank>$key</a>",$content,$rs[num]); //$content=str_replace_limit($key,"<a href=$value style=text-decoration:underline;font-size:14px;color:{$webdb[ShowKeywordColor]}; target=_blank>$key</a>",$content,2); //重复的关键字只能显示两次 $search[] = $key; $replace[] = "<a href={$value} style=text-decoration:underline;font-size:14px;color:{$webdb[ShowKeywordColor]}; target=_blank>{$key}</a>"; } $search && ($content = str_replace_limit($search, $replace, $content, 2)); return $content; }
static function parse_multimark(&$sql, &$params) { if (is_bool(strpos($sql, '??'))) { return; } $sql = str_replace('??', 'MULTIMARK', $sql); $sql = explode('?', $sql); $parsed_params = array(); foreach ($sql as &$part) { $current_param = current($params); next($params); if (is_bool(strpos($part, 'MULTIMARK'))) { $parsed_params[] = $current_param; continue; } if (!is_array($current_param)) { throw new Exception('DB::parse_multimark: Parameter passed is not an array.'); } $parsed_params = array_merge($parsed_params, $current_param); $marks = implode(', ', array_fill(0, count($current_param), '?')); $part = str_replace_limit('MULTIMARK', $marks, $part); unset($part); } $params = $parsed_params; $sql = implode('?', $sql); }
/** * * @param object $object_ * @param array $arrayFilter * @return array * Extract object data to return an array */ function extractData($object_, array $arrayFilter = array(), $multiple = false) { $data = null; if (!$multiple) { foreach (get_class_methods($object_) as $key => $value) { if (strpos($value, 'get') !== false) { if (method_exists($object_, $value)) { $method = $object_->{$value}(); if (!is_null($method)) { $key = strtolower(str_replace_limit('_', '', preg_replace('/([A-Z])/', '_$1', str_replace('get', '', $value)), 1)); $data[$key] = $object_->{$value}(); } } } } } elseif ($multiple) { $temp = null; foreach ($object_ as $object) { foreach (get_class_methods($object) as $key => $value) { if (strpos($value, 'get') !== false) { if (method_exists($object, $value)) { $method = $object->{$value}(); if (!is_null($method)) { $key = strtolower(str_replace_limit('_', '', preg_replace('/([A-Z])/', '_$1', str_replace('get', '', $value)), 1)); $temp[$key] = $object->{$value}(); } } } } $data[] = $temp; } } return $data; }
function isset_array($arr, &$var_to_check = null) { $arr = str_replace(array('[', ']'), array("['", "']"), $arr); $result = null; if ($var_to_check) { $ext_var = (array) $var_to_check; } else { $var_to_check = substr($arr, 0, strpos($arr, '[')); global ${$var_to_check}; if (!isset(${$var_to_check})) { return; } $ext_var = (array) ${$var_to_check}; $arr = str_replace_limit($var_to_check, '', $arr); } eval('isset($ext_var' . $arr . ') && $result = $ext_var' . $arr . ';'); return $result; }