/** * @covers Patchwork\PHP\Override\Mbstring::mb_strpos * @covers Patchwork\PHP\Override\Mbstring::mb_stripos * @covers Patchwork\PHP\Override\Mbstring::mb_strrpos * @covers Patchwork\PHP\Override\Mbstring::mb_strripos */ function testmb_strpos() { $this->assertSame(false, @mb_strpos('abc', '')); $this->assertSame(false, @mb_strpos('abc', 'a', -1)); $this->assertSame(false, mb_strpos('abc', 'd')); $this->assertSame(false, mb_strpos('abc', 'a', 3)); $this->assertSame(1, mb_strpos('한국어', '국')); $this->assertSame(3, mb_stripos('DÉJÀ', 'à')); $this->assertSame(false, mb_strrpos('한국어', '')); $this->assertSame(1, mb_strrpos('한국어', '국')); $this->assertSame(3, mb_strripos('DÉJÀ', 'à')); $this->assertSame(1, mb_stripos('aςσb', 'ΣΣ')); $this->assertSame(1, mb_strripos('aςσb', 'ΣΣ')); $this->assertSame(false, @p::mb_strpos('abc', '')); $this->assertSame(false, @p::mb_strpos('abc', 'a', -1)); $this->assertSame(false, p::mb_strpos('abc', 'd')); $this->assertSame(false, p::mb_strpos('abc', 'a', 3)); $this->assertSame(1, p::mb_strpos('한국어', '국')); $this->assertSame(3, p::mb_stripos('DÉJÀ', 'à')); $this->assertSame(false, p::mb_strrpos('한국어', '')); $this->assertSame(1, p::mb_strrpos('한국어', '국')); $this->assertSame(3, p::mb_strripos('DÉJÀ', 'à')); $this->assertSame(1, p::mb_stripos('aςσb', 'ΣΣ')); $this->assertSame(1, p::mb_strripos('aςσb', 'ΣΣ')); }
protected function validateEquals($input) { if (is_array($input)) { return end($input) == $this->endValue; } return mb_strripos($input, $this->endValue, -1, $enc = mb_detect_encoding($input)) === mb_strlen($input, $enc) - mb_strlen($this->endValue, $enc); }
/** * LastIndexOf */ public function execute() { if ($this->isCaseInsensitive()) { return mb_strripos($this->getValue(), $this->needle, $this->fromIndex); } else { return mb_strrpos($this->getValue(), $this->needle, $this->fromIndex); } }
public function formatShortString($string, $maxlen = 200, $separator = '...') { $string = preg_replace('#<(?:br|p|hr|div)#', ' $0', $string); $string = strip_tags($string); $len = mb_strlen($string, 'UTF-8') > $maxlen ? mb_strripos(mb_substr($string, 0, $maxlen, 'UTF-8'), ' ', 0, 'UTF-8') : $maxlen; $cutStr = mb_substr($string, 0, $len, 'UTF-8'); return mb_strlen($string, 'UTF-8') > $maxlen ? '' . $cutStr . $separator : '' . $cutStr . ''; }
function cutString($string, $maxlen) { //обрезает красиво строку, убирает теги, добавляя ... в конце $string = strip_tags($string); $len = mb_strlen($string) > $maxlen ? mb_strripos(mb_substr($string, 0, $maxlen), ' ') : $maxlen; $cutStr = mb_substr($string, 0, $len); return mb_strlen($string) > $maxlen ? $cutStr . '...' : $cutStr; }
public static function utf8_strripos($haystack, $needle, $offset) { if (UTF8_MBSTRING) { self::_fixMbStrrposPhp52($haystack, $offset); return mb_strripos($haystack, $needle, $offset); } else { return strripos($haystack, $needle, $offset); } }
/** * @param Response $response * @param string $editorHtml */ private function injectEditor(Response $response, $editorHtml) { $html = $response->getContent(); $pos = mb_strripos($html, '</body>'); if (false === $pos) { return; } $html = mb_substr($html, 0, $pos) . $editorHtml . mb_substr($html, $pos); $response->setContent($html); }
protected function injectCookieBar(Response $response) { $content = $response->getContent(); $pos = mb_strripos($content, '</body>'); if (false !== $pos) { $toolbar = sprintf("\n%s\n", $this->cookieService->render()); $content = mb_substr($content, 0, $pos) . $toolbar . mb_substr($content, $pos); $response->setContent($content); } }
protected function fixEmbeddedAttachments($message, $oldID, $newID) { if (mb_strripos($message, '[attach]' . $oldID . '[/attach]') !== false || mb_strripos($message, '[attach=' . $oldID . ']') !== false || mb_strripos($message, '[attach=' . $oldID . ',') !== false) { $message = str_ireplace('[attach]' . $oldID . '[/attach]', '[attach]' . $newID . '[/attach]', $message); $message = str_ireplace('[attach=' . $oldID . ']', '[attach=' . $newID . ']', $message); $message = str_ireplace('[attach=' . $oldID . ',', '[attach=' . $newID . ',', $message); return $message; } return false; }
/** * Генерация слов по которым найдена акция * @param $news * @return $this */ public function get_words($news) { $words = array(); foreach ($this->_original_words as $word) { if (mb_strripos($news->title, $word) !== FALSE or mb_strripos($news->text, $word) !== FALSE) { $words[] = $word; } } $words = array_unique($words); return View::factory('frontend/search/search_words')->set('words', $words); }
/** * Обрезание текста по длине с поиском последнего полностью вмещающегося слова и удалением лишних крайних знаков пунктуации. * * @author Agel_Nash <*****@*****.**> * @version 0.1 * * @param string $html HTML текст * @param integer $len максимальная длина строки * @param string $encoding кодировка * @return string */ public static function mb_trim_word($html, $len, $encoding = 'UTF-8') { $text = trim(preg_replace('|\\s+|', ' ', strip_tags($html))); $text = mb_substr($text, 0, $len + 1, $encoding); if (mb_substr($text, -1, null, $encoding) == ' ') { $out = trim($text); } else { $out = mb_substr($text, 0, mb_strripos($text, ' ', null, $encoding), $encoding); } return preg_replace("/(([\\.,\\-:!?;\\s])|(&\\w+;))+\$/ui", "", $out); }
/** * @param \Symfony\Component\HttpFoundation\Response $response */ protected function injectToolbar(Response $response) { $content = $response->getContent(); $pos = mb_strripos($content, '</body>'); if (FALSE !== $pos) { if ($token = $response->headers->get('X-Debug-Token')) { $loader = ['#theme' => 'webprofiler_loader', '#token' => $token, '#profiler_url' => $this->urlGenerator->generate('webprofiler.toolbar', ['profile' => $token])]; $content = mb_substr($content, 0, $pos) . $this->renderer->renderRoot($loader) . mb_substr($content, $pos); $response->setContent($content); } } }
function xyz_fbap_string_limit($string, $limit) { $space = " "; $appendstr = " ..."; if (mb_strlen($string) <= $limit) { return $string; } if (mb_strlen($appendstr) >= $limit) { return ''; } $string = mb_substr($string, 0, $limit - mb_strlen($appendstr)); $rpos = mb_strripos($string, $space); if ($rpos === false) { return $string . $appendstr; } else { return mb_substr($string, 0, $rpos) . $appendstr; } }
/** * Soft cutting for string * * @param string $string Source string * @param integer $soft Soft cutting position * @param integer $hard Hard cutting position * @param integer $strategy Cutting strategy [optional] * @param boolean $strip Strip tags in source [optional] * @param string $encoding Source encoding [optional] * @return string Cutted string */ public static function softCut($string, $soft, $hard, $strategy = self::CUT_STRATEGY_MIN, $strip = true, $encoding = 'utf-8') { if ($strip) { $string = strip_tags($string); } if (mb_strlen($string) <= $soft) { return $string; } $cut = mb_substr($string, 0, $hard, $encoding); $pos = $strategy == self::CUT_STRATEGY_MIN ? mb_stripos($cut, ' ', $soft, $encoding) : mb_strripos($cut, ' ', $soft, $encoding); if ($pos === false) { $pos = $strategy == self::CUT_STRATEGY_MIN ? mb_stripos($cut, ' ', $soft, $encoding) : mb_strripos($cut, ' ', $soft, $encoding); } if ($pos === false) { $pos = $strategy == self::CUT_STRATEGY_MIN ? mb_stripos($cut, ' ', $soft, $encoding) : mb_strripos($cut, ' ', $soft, $encoding); } if ($pos === false) { return $cut; } return mb_substr($string, 0, $pos, $encoding); }
private static function grapheme_position($s, $needle, $offset, $mode) { if (!preg_match('/./us', $needle .= '')) { return false; } if (!preg_match('/./us', $s .= '')) { return false; } if ($offset > 0) { $s = self::grapheme_substr($s, $offset); } elseif ($offset < 0) { $offset = 0; } switch ($mode) { case 0: $needle = iconv_strpos($s, $needle, 0, 'UTF-8'); break; case 1: $needle = mb_stripos($s, $needle, 0, 'UTF-8'); break; case 2: $needle = iconv_strrpos($s, $needle, 'UTF-8'); break; default: $needle = mb_strripos($s, $needle, 0, 'UTF-8'); break; } return $needle ? self::grapheme_strlen(iconv_substr($s, 0, $needle, 'UTF-8')) + $offset : $needle; }
<?php $string = '<body>Umlauttest öüä</body>'; var_dump(strlen($string)); var_dump(mb_strlen($string)); var_dump(strripos($string, '</body>')); var_dump(mb_strripos($string, '</body>'));
/** * Generates parameters for the GIS data editor from the value of the GIS column. * This method performs common work. * More specific work is performed by each of the geom classes. * * @param string $value value of the GIS column * * @return array parameters for the GIS editor from the value of the GIS column * @access protected */ protected function generateParams($value) { $geom_types = '(POINT|MULTIPOINT|LINESTRING|MULTILINESTRING' . '|POLYGON|MULTIPOLYGON|GEOMETRYCOLLECTION)'; $srid = 0; $wkt = ''; if (preg_match("/^'" . $geom_types . "\\(.*\\)',[0-9]*\$/i", $value)) { $last_comma = mb_strripos($value, ","); $srid = trim(mb_substr($value, $last_comma + 1)); $wkt = trim(mb_substr($value, 1, $last_comma - 2)); } elseif (preg_match("/^" . $geom_types . "\\(.*\\)\$/i", $value)) { $wkt = $value; } return array('srid' => $srid, 'wkt' => $wkt); }
var_dump(strrpos("abc abc abc", "abc", -1)); var_dump(strrpos("abc abc abc", "abc", -3)); var_dump(strrpos("abc abc abc", "abc", -6)); var_dump(mb_strrpos("●○◆ ●○◆ ●○◆", "●○◆", 0)); var_dump(mb_strrpos("●○◆ ●○◆ ●○◆", "●○◆", 3)); var_dump(mb_strrpos("●○◆ ●○◆ ●○◆", "●○◆", 6)); var_dump(mb_strrpos("●○◆ ●○◆ ●○◆", "●○◆", 9)); var_dump(mb_strrpos("●○◆ ●○◆ ●○◆", "●○◆", 11)); var_dump(mb_strrpos("●○◆ ●○◆ ●○◆", "●○◆", 12)); var_dump(mb_strrpos("●○◆ ●○◆ ●○◆", "●○◆", -1)); var_dump(mb_strrpos("●○◆ ●○◆ ●○◆", "●○◆", -3)); var_dump(mb_strrpos("●○◆ ●○◆ ●○◆", "●○◆", -6)); var_dump(strripos("abc abc abc", "abc", 0)); var_dump(strripos("abc abc abc", "abc", 3)); var_dump(strripos("abc abc abc", "abc", 6)); var_dump(strripos("abc abc abc", "abc", 9)); var_dump(strripos("abc abc abc", "abc", 11)); var_dump(strripos("abc abc abc", "abc", 12)); var_dump(strripos("abc abc abc", "abc", -1)); var_dump(strripos("abc abc abc", "abc", -3)); var_dump(strripos("abc abc abc", "abc", -6)); var_dump(mb_strripos("●○◆ ●○◆ ●○◆", "●○◆", 0)); var_dump(mb_strripos("●○◆ ●○◆ ●○◆", "●○◆", 3)); var_dump(mb_strripos("●○◆ ●○◆ ●○◆", "●○◆", 6)); var_dump(mb_strripos("●○◆ ●○◆ ●○◆", "●○◆", 9)); var_dump(mb_strripos("●○◆ ●○◆ ●○◆", "●○◆", 11)); var_dump(mb_strripos("●○◆ ●○◆ ●○◆", "●○◆", 12)); var_dump(mb_strripos("●○◆ ●○◆ ●○◆", "●○◆", -1)); var_dump(mb_strripos("●○◆ ●○◆ ●○◆", "●○◆", -3)); var_dump(mb_strripos("●○◆ ●○◆ ●○◆", "●○◆", -6));
/** * Grab the index of the last matched character. * * @param string $string * @param string $needle * @param bool $strict * @param int $offset * @return int */ public static function lastIndexOf($string, $needle, $strict = true, $offset = 0) { if ($strict) { return mb_strrpos($string, $needle, $offset); } return mb_strripos($string, $needle, $offset); }
/** * testUsingMbStrripos method * * @return void */ public function testUsingMbStrripos() { $string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $find = 'F'; $result = mb_strripos($string, $find); $expected = 5; $this->assertEquals($expected, $result); $string = 'ABCDEFGHIJKLMNOPQFRSTUVWXYZ0123456789'; $find = 'F'; $result = mb_strripos($string, $find, 6); $expected = 17; $this->assertEquals($expected, $result); $string = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ'; $find = 'Å'; $result = mb_strripos($string, $find); $expected = 5; $this->assertEquals($expected, $result); $string = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÅÙÚÛÜÝÞ'; $find = 'Å'; $result = mb_strripos($string, $find, 6); $expected = 24; $this->assertEquals($expected, $result); $string = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÅÙÚÛÜÝÞ'; $find = 'ÓÔ'; $result = mb_strripos($string, $find); $expected = 19; $this->assertEquals($expected, $result); $string = 'ĀĂĄĆĈĊČĎĐĒĔĖĘĚĜĞĠĢĤĦĨĪĬĮIJĴĶĹĻĽĿŁŃŅŇŊŌŎŐŒŔŖŘŚŜŞŠŢŤŦŨŪŬŮŰŲŴŶŹŻŽ'; $find = 'Ċ'; $result = mb_strripos($string, $find); $expected = 5; $this->assertEquals($expected, $result); $string = 'ĀĂĄĆĈĊČĎĐĒĔĖĘĚĜĞĠĢĤĦĨĪĬĮIJĴĶĹĻĽĿŁĊŃŅŇŊŌŎŐŒŔŖŘŚŜŞŠŢŤŦŨŪŬŮŰŲŴŶŹŻŽ'; $find = 'Ċ'; $result = mb_strripos($string, $find, 6); $expected = 32; $this->assertEquals($expected, $result); $string = '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'; $find = 'F'; $result = mb_strripos($string, $find); $expected = 69; $this->assertEquals($expected, $result); $string = '¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈ'; $find = 'µ'; $result = mb_strripos($string, $find); $expected = 20; $this->assertEquals($expected, $result); $string = 'ÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬ'; $find = 'é'; $result = mb_strripos($string, $find); $expected = 32; $this->assertEquals($expected, $result); $string = 'ĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽžſƀƁƂƃƄƅƆƇƈƉƊƋƌƍƎƏƐ'; $find = 'Ņ'; $result = mb_strripos($string, $find); $expected = 25; $this->assertEquals($expected, $result); $string = 'ƑƒƓƔƕƖƗƘƙƚƛƜƝƞƟƠơƢƣƤƥƦƧƨƩƪƫƬƭƮƯưƱƲƳƴƵƶƷƸƹƺƻƼƽƾƿǀǁǂǃDŽDždžLJLjljNJNjnjǍǎǏǐǑǒǓǔǕǖǗǘǙǚǛǜǝǞǟǠǡǢǣǤǥǦǧǨǩǪǫǬǭǮǯǰDZDzdzǴ'; $find = 'Ƹ'; $result = mb_strripos($string, $find); $expected = 40; $this->assertEquals($expected, $result); $string = 'ƑƒƓƔƕƖƗƘƙƚƛƜƝƞƟƠơƢƣƤƥƦƧƨƩƪƫƬƭƮƯưƱƲƳƴƵƶƷƸƹƺƻƼƽƾƿǀǁǂǃDŽDždžLJLjljNJNjnjǍǎǏǐǑǒǓǔǕǖǗǘǙǚǛǜǝǞǟǠǡǢǣǤǥǦǧǨǩǪǫǬǭǮǯǰDZDzdzǴ'; $find = 'ƹ'; $result = mb_strripos($string, $find); $expected = 40; $this->assertEquals($expected, $result); $string = 'əɚɛɜɝɞɟɠɡɢɣɤɥɦɧɨɩɪɫɬɭɮɯɰɱɲɳɴɵɶɷɸɹɺɻɼɽɾɿʀʁʂʃʄʅʆʇʈʉʊʋʌʍʎʏʐʑʒʓʔʕʖʗʘʙʚʛʜʝʞʟʠʡʢʣʤʥʦʧʨʩʪʫʬʭʮʯʰʱʲʳʴʵʶʷʸʹʺʻʼ'; $find = 'ʀ'; $result = mb_strripos($string, $find); $expected = 39; $this->assertEquals($expected, $result); $string = 'ЀЁЂЃЄЅІЇЈЉЊЋЌЍЎЏАБВГДЕЖЗИЙКЛ'; $find = 'Ї'; $result = mb_strripos($string, $find); $expected = 7; $this->assertEquals($expected, $result); $string = 'МНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь'; $find = 'Р'; $result = mb_strripos($string, $find); $expected = 36; $this->assertEquals($expected, $result); $string = 'МНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь'; $find = 'р'; $result = mb_strripos($string, $find, 5); $expected = 36; $this->assertEquals($expected, $result); $string = 'فقكلمنهوىيًٌٍَُ'; $find = 'ن'; $result = mb_strripos($string, $find); $expected = 5; $this->assertEquals($expected, $result); $string = '✰✱✲✳✴✵✶✷✸✹✺✻✼✽✾✿❀❁❂❃❄❅❆❇❈❉❊❋❌❍❎❏❐❑❒❓❔❕❖❗❘❙❚❛❜❝❞'; $find = '✿'; $result = mb_strripos($string, $find); $expected = 15; $this->assertEquals($expected, $result); $string = '⺀⺁⺂⺃⺄⺅⺆⺇⺈⺉⺊⺋⺌⺍⺎⺏⺐⺑⺒⺓⺔⺕⺖⺗⺘⺙⺛⺜⺝⺞⺟⺠⺡⺢⺣⺤⺥⺦⺧⺨⺩⺪⺫⺬⺭⺮⺯⺰⺱⺲⺳⺴⺵⺶⺷⺸⺹⺺⺻⺼⺽⺾⺿⻀⻁⻂⻃⻄⻅⻆⻇⻈⻉⻊⻋⻌⻍⻎⻏⻐⻑⻒⻓⻔⻕⻖⻗⻘⻙⻚⻛⻜⻝⻞⻟⻠'; $find = '⺐'; $result = mb_strripos($string, $find); $expected = 16; $this->assertEquals($expected, $result); $string = '⽅⽆⽇⽈⽉⽊⽋⽌⽍⽎⽏⽐⽑⽒⽓⽔⽕⽖⽗⽘⽙⽚⽛⽜⽝⽞⽟⽠⽡⽢⽣⽤⽥⽦⽧⽨⽩⽪⽫⽬⽭⽮⽯⽰⽱⽲⽳⽴⽵⽶⽷⽸⽹⽺⽻⽼⽽⽾⽿'; $find = '⽤'; $result = mb_strripos($string, $find); $expected = 31; $this->assertEquals($expected, $result); $string = '눡눢눣눤눥눦눧눨눩눪눫눬눭눮눯눰눱눲눳눴눵눶눷눸눹눺눻눼눽눾눿뉀뉁뉂뉃뉄뉅뉆뉇뉈뉉뉊뉋뉌뉍뉎뉏뉐뉑뉒뉓뉔뉕뉖뉗뉘뉙뉚뉛뉜뉝뉞뉟뉠뉡뉢뉣뉤뉥뉦뉧뉨뉩뉪뉫뉬뉭뉮뉯뉰뉱뉲뉳뉴뉵뉶뉷뉸뉹뉺뉻뉼뉽뉾뉿늀늁늂늃늄'; $find = '눻'; $result = mb_strripos($string, $find); $expected = 26; $this->assertEquals($expected, $result); $string = 'ﹰﹱﹲﹳﹴﹶﹷﹸﹹﹺﹻﹼﹽﹾﹿﺀﺁﺂﺃﺄﺅﺆﺇﺈﺉﺊﺋﺌﺍﺎﺏﺐﺑﺒﺓﺔﺕﺖﺗﺘﺙﺚﺛﺜﺝﺞﺟﺠﺡﺢﺣﺤﺥﺦﺧﺨﺩﺪﺫﺬﺭﺮﺯﺰ'; $find = 'ﺞ'; $result = mb_strripos($string, $find); $expected = 46; $this->assertEquals($expected, $result); $string = 'ﺱﺲﺳﺴﺵﺶﺷﺸﺹﺺﺻﺼﺽﺾﺿﻀﻁﻂﻃﻄﻅﻆﻇﻈﻉﻊﻋﻌﻍﻎﻏﻐﻑﻒﻓﻔﻕﻖﻗﻘﻙﻚﻛﻜﻝﻞﻟﻠﻡﻢﻣﻤﻥﻦﻧﻨﻩﻪﻫﻬﻭﻮﻯﻰﻱﻲﻳﻴﻵﻶﻷﻸﻹﻺﻻﻼ'; $find = 'ﻞ'; $result = mb_strripos($string, $find); $expected = 45; $this->assertEquals($expected, $result); $string = 'abcdefghijklmnopqrstuvwxyz'; $find = 'k'; $result = mb_strripos($string, $find); $expected = 10; $this->assertEquals($expected, $result); $string = 'abcdefghijklmnopqrstuvwxyz'; $find = 'k'; $result = mb_strripos($string, $find); $expected = 10; $this->assertEquals($expected, $result); $string = 'abcdefghijklmnoppqrstuvwxyz'; $find = 'pp'; $result = mb_strripos($string, $find); $expected = 15; $this->assertEquals($expected, $result); $string = '。「」、・ヲァィゥェォャュョッーアイウエオカキク'; $find = 'ア'; $result = mb_strripos($string, $find); $expected = 16; $this->assertEquals($expected, $result); $string = 'ケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン゙'; $find = 'ハ'; $result = mb_strripos($string, $find); $expected = 17; $this->assertEquals($expected, $result); $string = 'Ĥēĺļŏ, Ŵőřļď!'; $find = 'ő'; $result = mb_strripos($string, $find); $expected = 8; $this->assertEquals($expected, $result); $string = 'Ĥēĺļŏ, Ŵőřļď!'; $find = 'ő'; $result = mb_strripos($string, $find); $expected = 8; $this->assertEquals($expected, $result); $string = 'Hello, World!'; $find = 'o'; $result = mb_strripos($string, $find); $expected = 8; $this->assertEquals($expected, $result); $string = 'Hello, World!'; $find = 'o'; $result = mb_strripos($string, $find, 5); $expected = 8; $this->assertEquals($expected, $result); $string = 'čini'; $find = 'n'; $result = mb_strripos($string, $find); $expected = 2; $this->assertEquals($expected, $result); $string = 'čini'; $find = 'n'; $result = mb_strripos($string, $find); $expected = 2; $this->assertEquals($expected, $result); $string = 'moći'; $find = 'ć'; $result = mb_strripos($string, $find); $expected = 2; $this->assertEquals($expected, $result); $string = 'moći'; $find = 'ć'; $result = mb_strripos($string, $find); $expected = 2; $this->assertEquals($expected, $result); $string = 'državni'; $find = 'ž'; $result = mb_strripos($string, $find); $expected = 2; $this->assertEquals($expected, $result); $string = '把百度设为首页'; $find = '设'; $result = mb_strripos($string, $find); $expected = 3; $this->assertEquals($expected, $result); $string = '一二三周永龍'; $find = '周'; $result = mb_strripos($string, $find); $expected = 3; $this->assertEquals($expected, $result); $string = 'državni'; $find = 'dž'; $result = mb_strripos($string, $find); $this->assertFalse($result); }
protected static function grapheme_position($s, $needle, $offset, $mode) { if ($offset > 0) { $s = (string) self::grapheme_substr($s, $offset); } else { if ($offset < 0) { $offset = 0; } } if ('' === (string) $needle) { return false; } if ('' === (string) $s) { return false; } switch ($mode) { case 0: $needle = iconv_strpos($s, $needle, 0, 'UTF-8'); break; case 1: $needle = mb_stripos($s, $needle, 0, 'UTF-8'); break; case 2: $needle = iconv_strrpos($s, $needle, 'UTF-8'); break; default: $needle = mb_strripos($s, $needle, 0, 'UTF-8'); break; } return $needle ? self::grapheme_strlen(iconv_substr($s, 0, $needle, 'UTF-8')) + $offset : $needle; }
/** * Checks if the String [$haystack] ends with [$needle] * * @param string $haystack Input string. * @param string $needle Needed char or string * @param bool $caseSensitive * @return bool */ function endsWith($haystack, $needle, $caseSensitive = false) { if ($caseSensitive) { return mb_strrpos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle); } return mb_strripos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle); }
<?php /* Prototype : int mb_strripos(string haystack, string needle [, int offset [, string encoding]]) * Description: Finds position of last occurrence of a string within another, case insensitive * Source code: ext/mbstring/mbstring.c * Alias to functions: */ /* * Test how mb_strripos behaves when passed an incorrect number of arguments */ echo "*** Testing mb_strripos() : error conditions ***\n"; //Test mb_strripos with one more than the expected number of arguments echo "\n-- Testing mb_strripos() function with more than expected no. of arguments --\n"; $haystack = 'string_val'; $needle = 'string_val'; $offset = 10; $encoding = 'string_val'; $extra_arg = 10; var_dump(mb_strripos($haystack, $needle, $offset, $encoding, $extra_arg)); // Testing mb_strripos with one less than the expected number of arguments echo "\n-- Testing mb_strripos() function with less than expected no. of arguments --\n"; $haystack = 'string_val'; var_dump(mb_strripos($haystack)); echo "Done";
private function parseQuery($query) { $groupBy = array(); $orderBy = array(); $orderDir = array(); $wheres = array(); $limit = 0; $offset = 0; $query = preg_replace('/\\s+/u', ' ', $query); $query = preg_replace('/[\\)`\\s]from[\\(`\\s]/ui', ' FROM ', $query); if (preg_match('/(limit([0-9\\s\\,]+)){1}$/ui', $query, $matches)) { $query = str_ireplace(Arrays::first($matches), '', $query); $tmp = explode(',', $matches[2]); if (isset($tmp[1])) { $offset = (int) trim(Arrays::first($tmp)); $limit = (int) trim($tmp[1]); } else { $offset = 0; $limit = (int) trim(Arrays::first($tmp)); } } if (preg_match('/(order\\sby([^\\(\\)]+)){1}$/ui', $query, $matches)) { $query = str_ireplace(Arrays::first($matches), '', $query); $tmp = explode(',', $matches[2]); foreach ($tmp as $item) { $item = trim($item); $direct = mb_strripos($item, ' desc') == mb_strlen($item) - 5 || mb_strripos($item, '`desc') == mb_strlen($item) - 5 ? 'desc' : 'asc'; $item = str_ireplace(array(' asc', ' desc', '`asc', '`desc', '`'), '', $item); $orderBy[] = $item; $orderDir[] = Inflector::upper($direct); } } if (preg_match('/(group\\sby([^\\(\\)]+)){1}$/ui', $query, $matches)) { $query = str_ireplace(Arrays::first($matches), '', $query); $tmp = explode(',', $matches[2]); foreach ($tmp as $item) { $item = trim($item); $groupBy[] = $item; } } $tmp = preg_replace_callback('/\\( (?> [^)(]+ | (?R) )+ \\)/xui', array($this, 'queryParamsCallback'), $query); $words = explode(' ', $query); $method = Inflector::lower(Arrays::first($words)); $parts = explode(' where ', Inflector::lower($query)); if (2 == count($parts)) { $whs = Arrays::last($parts); $whs = str_replace(array(' and ', ' or ', ' xor ', ' && ', ' || ', ' | '), array(' AND ', ' OR ', ' XOR ', ' AND ', ' OR ', ' XOR '), $whs); $wheres['AND'] = strstr($whs, ' AND ') ? explode(' AND ', $whs) : array(); $wheres['OR'] = strstr($whs, ' OR ') ? explode(' OR ', $whs) : array(); $wheres['XOR'] = strstr($whs, ' XOR ') ? explode(' XOR ', $whs) : array(); } return array('method' => $method, 'wheres' => $wheres, 'groupBy' => $groupBy, 'orderBy' => $orderBy, 'orderDir' => $orderDir, 'limit' => $limit, 'offset' => $offset); }
protected function parse_query_params() { $query = preg_replace('/\\s+/u', ' ', $this->query); $query = preg_replace('/[\\)`\\s]from[\\(`\\s]/ui', ' FROM ', $query); if (preg_match('/(limit([0-9\\s\\,]+)){1}$/ui', $query, $matches)) { $query = str_ireplace($matches[0], '', $query); if (!$this->ajax_request) { $tmp = explode(',', $matches[2]); if (isset($tmp[1])) { $this->start = (int) trim($tmp[0]); $this->limit = (int) trim($tmp[1]); } else { $this->start = 0; $this->limit = (int) trim($tmp[0]); } } } if (preg_match('/(order\\sby([^\\(\\)]+)){1}$/ui', $query, $matches)) { $query = str_ireplace($matches[0], '', $query); if (!$this->ajax_request) { $tmp = explode(',', $matches[2]); foreach ($tmp as $item) { $item = trim($item); $direct = mb_strripos($item, ' desc') == mb_strlen($item) - 5 || mb_strripos($item, '`desc') == mb_strlen($item) - 5 ? 'desc' : 'asc'; $item = str_ireplace(array(' asc', ' desc', '`asc', '`desc', '`'), '', $item); $this->order_by[$item] = $direct; } } } $tmp = preg_replace_callback('/\\( (?> [^)(]+ | (?R) )+ \\)/xui', array($this, 'query_params_callback'), $query); $from_pos = mb_strpos($tmp, ' FROM '); $this->total_query = mb_substr($query, $from_pos); $query = mb_substr($query, 0, $from_pos) . ',(0) AS `primary_key`' . mb_substr($query, $from_pos); return $query; }
/** * Finds the position of last occurrence (case insensitive) of a string in a string. * @param string $haystack The string from which to get the position of the last occurrence. * @param string $needle The string to be found. * @param int $offset (optional) $offset may be specified to begin searching an arbitrary position. Negative values will stop searching at an arbitrary point prior to the end of the string. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default. * @return mixed Returns the numeric position of the first occurrence (case insensitive) of $needle in the $haystack, or FALSE if $needle is not found. * Note: The first character's position is 0, the second character position is 1, and so on. * This function is aimed at replacing the functions strripos() and mb_strripos() for human-language strings. * @link http://php.net/manual/en/function.strripos * @link http://php.net/manual/en/function.mb-strripos */ function api_strripos($haystack, $needle, $offset = 0, $encoding = null) { return mb_strripos($haystack, $needle, $offset, $encoding); }
echo "*** Testing mb_strripos() : basic functionality***\n"; mb_internal_encoding('UTF-8'); //ascii strings $ascii_haystacks = array('abc defabc def', 'ABC DEFABC DEF', 'Abc dEFaBC Def'); $ascii_needles = array('DE', 'de', 'De', 'dE'); //greek strings in UTF-8 $greek_lower = base64_decode('zrrOu868zr3Ovs6/z4DPgSDOus67zrzOvc6+zr/PgA=='); $greek_upper = base64_decode('zprOm86czp3Ons6fzqDOoSDOms6bzpzOnc6ezp/OoA=='); $greek_mixed = base64_decode('zrrOu868zr3Ovs6fzqDOoSDOus67zpzOnc6+zr/OoA=='); $greek_haystacks = array($greek_lower, $greek_upper, $greek_mixed); $greek_nlower = base64_decode('zrzOvc6+zr8='); $greek_nupper = base64_decode('zpzOnc6ezp8='); $greek_nmixed1 = base64_decode('zpzOnc6+zr8='); $greek_nmixed2 = base64_decode('zrzOvc6+zp8='); $greek_needles = array($greek_nlower, $greek_nupper, $greek_nmixed1, $greek_nmixed2); // try the basic options echo "\n -- ASCII Strings --\n"; foreach ($ascii_needles as $needle) { foreach ($ascii_haystacks as $haystack) { var_dump(mb_strripos($haystack, $needle)); var_dump(mb_strripos($haystack, $needle, 14)); } } echo "\n -- Greek Strings --\n"; foreach ($greek_needles as $needle) { foreach ($greek_haystacks as $haystack) { var_dump(mb_strripos($haystack, $needle)); var_dump(mb_strripos($haystack, $needle, 12)); } } echo "Done";
/** * Finds the last position (in characters) of the search value in the string - case is ignored when doing performing a match * * @param string $haystack The string to search in * @param string $needle The string to search for. This match will be done in a case-insensitive manner. * @param integer $offset The character position to start searching from. A negative value will stop looking that many characters from the end of the string * @return mixed The integer character position of the last occurence of the needle or `FALSE` if no match */ public static function irpos($haystack, $needle, $offset = 0) { // We get better performance falling back for ASCII strings if (!self::detect($haystack)) { return strripos($haystack, $needle, $offset); } if (self::$mbstring_available === NULL) { self::checkMbString(); } if (self::$mbstring_available && function_exists('mb_strripos')) { return mb_strripos($haystack, $needle, $offset, 'UTF-8'); } $haystack = self::lower($haystack); $needle = self::lower($needle); return self::rpos($haystack, $needle, $offset); }
/** * checks if the given DN is part of the given base DN(s) * @param string $dn the DN * @param string[] $bases array containing the allowed base DN or DNs * @return bool */ private function isDNPartOfBase($dn, $bases) { $belongsToBase = false; $bases = $this->sanitizeDN($bases); foreach ($bases as $base) { $belongsToBase = true; if (mb_strripos($dn, $base, 0, 'UTF-8') !== mb_strlen($dn, 'UTF-8') - mb_strlen($base, 'UTF-8')) { $belongsToBase = false; } if ($belongsToBase) { break; } } return $belongsToBase; }
/** * @see \wcf\data\ISearchAction::getSearchResultList() */ public function getSearchResultList() { $searchString = $this->parameters['data']['searchString']; $excludedSearchValues = array(); if (isset($this->parameters['data']['excludedSearchValues'])) { $excludedSearchValues = $this->parameters['data']['excludedSearchValues']; } $list = array(); if ($this->parameters['data']['includeUserGroups']) { $accessibleGroups = UserGroup::getAccessibleGroups(); foreach ($accessibleGroups as $group) { $groupName = $group->getName(); if (!in_array($groupName, $excludedSearchValues)) { $pos = mb_strripos($groupName, $searchString); if ($pos !== false && $pos == 0) { $list[] = array('label' => $groupName, 'objectID' => $group->groupID, 'type' => 'group'); } } } } // find users $userProfileList = new UserProfileList(); $userProfileList->getConditionBuilder()->add("username LIKE ?", array($searchString . '%')); if (!empty($excludedSearchValues)) { $userProfileList->getConditionBuilder()->add("username NOT IN (?)", array($excludedSearchValues)); } $userProfileList->sqlLimit = 10; $userProfileList->readObjects(); foreach ($userProfileList as $userProfile) { $list[] = array('icon' => $userProfile->getAvatar()->getImageTag(16), 'label' => $userProfile->username, 'objectID' => $userProfile->userID, 'type' => 'user'); } return $list; }