Skip to content

Commit

Permalink
Merge pull request #391 from tyrant88/tyrant88-patch-156
Browse files Browse the repository at this point in the history
use mb_ versions of string functions, add iconv ascii//IGNORE
  • Loading branch information
tyrant88 authored Jul 2, 2024
2 parents 0948d3f + 10a875d commit f196974
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
24 changes: 12 additions & 12 deletions functions/functions_search_it.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function search_it_getCategories($_ignoreoffline = true, $_onlyIDs = false, $_ca
if ($_onlyIDs) {
$return[] = $cat['id'];
} else {
$return[$cat['id']] = str_repeat(' ', substr_count($cat['path'], '|') * 2 - 2) . $cat['catname'];
$return[$cat['id']] = str_repeat(' ', mb_substr_count($cat['path'], '|') * 2 - 2) . $cat['catname'];
}

array_splice($cats, 0, 0, $sql->getArray(sprintf($query, $cat['id'])));
Expand All @@ -94,7 +94,7 @@ function search_it_getDirs($_startDir = '', $_getSubdirs = false): array
{
$si = rex_addon::get('search_it');

$startDepth = substr_count($_startDir, '/');
$startDepth = mb_substr_count($_startDir, '/');
if (@is_dir($_SERVER['DOCUMENT_ROOT'] . $_startDir)) {
$dirs2 = array_diff(scandir($_SERVER['DOCUMENT_ROOT'] . $_startDir), array('.', '..'));
} else {
Expand All @@ -114,7 +114,7 @@ function search_it_getDirs($_startDir = '', $_getSubdirs = false): array
while (!empty($dirs)) {
$dir = array_shift($dirs);

$depth = substr_count($dir, '/') - $startDepth;
$depth = mb_substr_count($dir, '/') - $startDepth;
if (@is_dir($_SERVER['DOCUMENT_ROOT'] . $dir) and $depth <= $si->getConfig('dirdepth')) {
$return[$_SERVER['DOCUMENT_ROOT'] . $dir] = $dir;
$subdirs = [];
Expand Down Expand Up @@ -143,7 +143,7 @@ function search_it_getFiles($_startDir = '', $_fileexts = [], $_getSubdirs = fal
$fileextPattern = '~\.([^.]+)$~is';
}

$startDepth = substr_count($_startDir, '/');
$startDepth = mb_substr_count($_startDir, '/');
if (@is_dir($_SERVER['DOCUMENT_ROOT'] . $_startDir)) {
$dirs2 = array_diff(scandir($_SERVER['DOCUMENT_ROOT'] . $_startDir), array('.', '..'));
} else {
Expand All @@ -165,7 +165,7 @@ function search_it_getFiles($_startDir = '', $_fileexts = [], $_getSubdirs = fal
while (!empty($dirs)) {
$dir = array_shift($dirs);

$depth = substr_count($dir, '/') - $startDepth;
$depth = mb_substr_count($dir, '/') - $startDepth;
if (@is_dir($_SERVER['DOCUMENT_ROOT'] . $dir) and $depth <= $si->getConfig('dirdepth')) {
$subdirs = [];
foreach (array_diff(scandir($_SERVER['DOCUMENT_ROOT'] . $dir), array('.', '..')) as $subdir) {
Expand Down Expand Up @@ -463,7 +463,7 @@ function search_it_config_unserialize($_str)
{
$conf = unserialize($_str);

if (strpos($_str, '\\"') === false) {
if (mb_strpos($_str, '\\"') === false) {
return $conf;
}

Expand Down Expand Up @@ -570,7 +570,7 @@ function soundex_ger($word)
$code = '';
$word = strtolower($word);

if (strlen($word) < 1) {
if (mb_strlen($word) < 1) {
return '';
}

Expand All @@ -580,12 +580,12 @@ function soundex_ger($word)
// Nur Buchstaben (keine Zahlen, keine Sonderzeichen)
$word = preg_replace('/[^a-zA-Z]/', '', $word);

$wordlen = strlen($word);
$wordlen = mb_strlen($word);
if (!$wordlen > 0) {
return '';
}

$char = str_split($word);
$char = mb_str_split($word);

// Sonderfälle bei Wortanfang (Anlaut)
if ($char[0] == 'c') {
Expand Down Expand Up @@ -719,9 +719,9 @@ function soundex_ger($word)
$code = preg_replace("/(.)\\1+/", "\\1", $code);
//echo "<br>code2: <b>" . $code . "</b><br />";
// entfernen aller Codes "0" ausser am Anfang
$codelen = strlen($code);
$codelen = mb_strlen($code);

$num = str_split($code);
$num = mb_str_split($code);
$phoneticcode = $num[0] ?? 0;

for ($x = 1; $x < $codelen; $x++) {
Expand Down Expand Up @@ -811,7 +811,7 @@ function search_it_getUrlAddOnTableName()
$allTables = $sql->getTables();

foreach ($allTables as $oneTable) {
if (strpos($oneTable, \Url\UrlManagerSql::TABLE_NAME) !== false) {
if (mb_strpos($oneTable, \Url\UrlManagerSql::TABLE_NAME) !== false) {
$tableName = $oneTable;
break;
}
Expand Down
25 changes: 12 additions & 13 deletions lib/search_it.php
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ private function getTeaserText($_text)
$return = '';
$aborted = false;
foreach ($textArray as $word) {
if ((($strlen = strlen($word)) + $i) > $this->maxTeaserChars) {
if ((($strlen = mb_strlen($word)) + $i) > $this->maxTeaserChars) {
$aborted = true;
break;
}
Expand Down Expand Up @@ -1328,7 +1328,7 @@ public function parseSearchString($_searchString): int
if ($notBlacklisted) {
// whitelisted words get extra weighted
$this->searchArray[$count] = array('search' => $word,
'weight' => strlen($plus) + 1 + (array_key_exists($word, $this->whitelist) ? $this->whitelist[$word] : 0),
'weight' => mb_strlen($plus) + 1 + (array_key_exists($word, $this->whitelist) ? $this->whitelist[$word] : 0),
'clang' => $this->clang
);
$count++;
Expand Down Expand Up @@ -1870,7 +1870,7 @@ public function getHighlightedText($_text)
for ($i = 0; $i < count($Apieces); $i++) {
if (preg_match('~(' . implode('|', $search) . ')~isu', $Apieces[$i])) {
break;
} elseif (preg_match('~(' . implode('|', $search) . ')~isu', str_replace(['\'', '"'], '', iconv("utf-8", "ascii//TRANSLIT", $Apieces[$i])))) {
} elseif (preg_match('~(' . implode('|', $search) . ')~isu', str_replace(['\'', '"'], '', iconv("utf-8", "ascii//TRANSLIT//IGNORE", $Apieces[$i])))) {
break;
}
}
Expand All @@ -1883,11 +1883,11 @@ public function getHighlightedText($_text)
preg_match('~^.*?(' . implode('|', $search) . ').{0,' . $this->maxHighlightedTextChars . '}~imsu', $return, $cutted);
if (!empty($cutted)) {
$needEllipses = false;
if (isset($cutted[1]) && strlen($cutted[1]) != strlen($return)) {
if (isset($cutted[1]) && mb_strlen($cutted[1]) != mb_strlen($return)) {
$needEllipses = true;
}

$return = preg_replace($replace, $this->surroundTags[0] . '$0' . $this->surroundTags[1], substr($cutted[0], 0, strrpos($cutted[0], ' ')));
$return = preg_replace($replace, $this->surroundTags[0] . '$0' . $this->surroundTags[1], mb_substr($cutted[0], 0, mb_strrpos($cutted[0], ' ')));

if ($needEllipses) {
$return .= ' ' . $this->ellipsis;
Expand All @@ -1908,15 +1908,14 @@ public function getHighlightedText($_text)
$replace[] = '~' . preg_quote($keyword['search'], '~') . '~isu';
}

$strlen = mb_strlen($_text);
$positions = [];
for ($i = 0; $i < count($tmp_searchArray); $i++) {
$hits = [];
$offset = 0;
preg_match_all('~((.{0,' . $this->maxHighlightedTextChars . '})' . preg_quote($tmp_searchArray[$i]['search'], '~') . '(.{0,' . $this->maxHighlightedTextChars . '}))~imsu', $_text, $hits, PREG_SET_ORDER);

foreach ($hits as $hit) {
$offset = strpos($_text, $hit[0], $offset) + 1;
$offset = mb_strpos($_text, $hit[0], $offset) + 1;
$currentposition = ceil(intval(($offset - 1) / (2 * $this->maxHighlightedTextChars)));

if ($this->highlightType == 'array' and !array_key_exists($tmp_searchArray[$i]['search'], $Ahighlighted)) {
Expand All @@ -1926,19 +1925,19 @@ public function getHighlightedText($_text)
if (trim($hit[1]) != '') {
$surroundText = $hit[1];

if (strlen($hit[2]) > 0 and false !== strpos($hit[2], ' ')) {
$surroundText = substr($surroundText, strpos($surroundText, ' '));
if (mb_strlen($hit[2]) > 0 and false !== mb_strpos($hit[2], ' ')) {
$surroundText = mb_substr($surroundText, mb_strpos($surroundText, ' '));
}

if (strlen($hit[3]) > 0 and false !== strpos($hit[3], ' ')) {
$surroundText = substr($surroundText, 0, strrpos($surroundText, ' '));
if (mb_strlen($hit[3]) > 0 and false !== mb_strpos($hit[3], ' ')) {
$surroundText = mb_substr($surroundText, 0, mb_strrpos($surroundText, ' '));
}

if ($i == 0 and strlen($hit[2]) > 0) {
if ($i == 0 and mb_strlen($hit[2]) > 0) {
$startEllipsis = true;
}

if ($i == (count($tmp_searchArray) - 1) and strlen($hit[3]) > 0) {
if ($i == (count($tmp_searchArray) - 1) and mb_strlen($hit[3]) > 0) {
$endEllipsis = true;
}

Expand Down

0 comments on commit f196974

Please sign in to comment.