changeStringByRegex($haystack,$regex1,$regex2,$firstConcatenateSymbol,$secondConcatenateSymbol,$requiredDelimiter); * @example-return * https://www.linija-svitla.ua/catalog/ulichnoe-osveshchenie?page=50&per-page=18&sort=test_test * ================================================================================================================| * @todo * 1) Пока что метод работает только с 2 regex,надо будет поменять строго regex string => regex array * 2) Метод полюбому обрезает первый символ результирующей строки regex * 3) нужно сделать механизм замены строки только по 1 regex * ================================================================================================================| * */ /** * @param string $haystack * @param string $regex1 * @param string $regex2 * @param string $requiredDelimiter * @param string $firstConcatenateSymbol * @param string $secondConcatenateSymbol * * @return string */ public static function changeStringByRegex(string $haystack, string $regex1, string $regex2, string $requiredDelimiter = '', string $firstConcatenateSymbol = '', string $secondConcatenateSymbol = '' ): string { # 1 give rexe1/regex2 parts # IF we have no consilience with both Regex == > return $haystack if (preg_match($regex1, $haystack) !== 0 || preg_match($regex2, $haystack) !== 0) { preg_match($regex1, $haystack, $matches[0]); preg_match($regex2, $haystack, $matches[1]); } else return $haystack; # 2 give must part of string $mustPartOfstring = self::SimpleStringSubstring($haystack, $requiredDelimiter); # 3 if regex1/regex2 !empty concatenate they with $mustPartOfString if (isset($matches[0][0]) && isset($matches[1][0])) { # удаляем первый символ ( прим; $matches[0][0]='&sort=test_desc') # нам надо только текст без первого спецсимвола $matches[0][0] = substr($matches[0][0], 1); $mustPartOfstring = (isset($matches[0][0])) ? $mustPartOfstring . $firstConcatenateSymbol . $matches[0][0] : $mustPartOfstring; $matches[1][0] = substr($matches[1][0], 1); $mustPartOfstring = (isset($matches[1][0])) ? $mustPartOfstring . $secondConcatenateSymbol . $matches[1][0] : $mustPartOfstring; } # если найден только 1й regex elseif (isset($matches[0][0]) && !isset($matches[1][0])) { $matches[0][0] = substr($matches[0][0], 1); $mustPartOfstring = (isset($matches[0][0])) ? $mustPartOfstring . $firstConcatenateSymbol . $matches[0][0] : $mustPartOfstring; } # если найден 2й regex elseif (!isset($matches[0][0]) && isset($matches[1][0])) { $matches[1][0] = substr($matches[1][0], 1); $mustPartOfstring = (isset($matches[1][0])) ? $mustPartOfstring . $firstConcatenateSymbol . $matches[1][0] : $mustPartOfstring; } return $mustPartOfstring; } }