NEW TRY!
idea: hardcoded replaceing

@param string $piece
@param int $space
@return string
761188
protected function _processSpaceErrors($piece, $space = 1) {
		$newPiece = $piece;
		$spaceChar = str_repeat(' ', $space);

		// At the beginning of the line
		if (mb_substr($newPiece, 0, $space) === $spaceChar && mb_substr($newPiece, $space, 1) === TB) {
			$newPiece = mb_substr($newPiece, $space);
		}
		// In the middle
		if (($pos = mb_strpos($newPiece, $space)) > 0 && mb_substr($newPiece, $pos - 1, 1) === TB
			&& mb_substr($newPiece, $pos + 1, 1) === TB) {
			$newPiece = mb_substr($newPiece, $pos) . mb_substr($newPiece, $pos + 2);
		}
		$newPiece = str_replace(TB . $spaceChar . TB, TB . TB, $newPiece);
		if ($newPiece !== $piece) {
			$this->_changes = true;
		}
		return $newPiece;
	}
1299596
static function implodeRecursive($glue, $pieces)
    {
        $items = [];
        foreach ($pieces as $item){
            if (is_array($item)){
                $items[] = self::implodeRecursive($glue, $item);
            }else{
                $items[] = $item;
            }
        }
        return implode($glue, $items);
    }
1341869
final private function resizeContent(string $content, int $space = 3):string
    {
        $maxLine = 0;
        $newString = "";
        foreach (preg_split("/((\r?\n)|(\r\n?))/", $content) as $line) {
            if ($maxLine < strlen($line)) {
                $maxLine = strlen($line);
            }
        }

        foreach (preg_split("/((\r?\n)|(\r\n?))/", $content) as $line) {
            $slength = strlen($line);
            $diff = abs($maxLine - $slength);


            $tmpStr = $this->copyContent(" ", $diff);
            $spaceRs = $this->copyContent(" ", $space);
            if ($space != 0) {
                $newString .= $spaceRs.$line.$tmpStr.$spaceRs."\n";
            } else {
                $newString .= "\n".$spaceRs.$line.$tmpStr.$spaceRs." ";
            }
        }
        return ($newString);
    }
866442
private function replace(
        ColumnMetadata $columnMetadata,
        string $searchTerm,
        string $replaceTerm,
        string $subject
    ): FieldReplaceResult {
        $fieldReplaced = null;
        foreach ($this->replacementStrategyList as $replacementStrategy) {
            if ($replacementStrategy->canReplace($searchTerm, $subject)) {
                $fieldReplaced = $replacementStrategy->replace(
                    $columnMetadata,
                    $searchTerm,
                    $replaceTerm,
                    $subject
                );
                break;
            }
        }

        if (is_null($fieldReplaced)) {
            throw new \RuntimeException('Error trying to replace "' . $searchTerm . '" with "' . $replaceTerm . '"');
        }

        return $fieldReplaced;
    }
312534
protected function splitSegment($segment)
    {
        $parts = preg_split('/\+(?<!\?\+)/', $segment);

        foreach ($parts as &$part) {
            $part = str_replace('?+', '+', $part);
        }

        return $parts;
    }
190813
public function Indent($code, $spaces)
    {
        $indent = "";
        
        for($i=0; $i<$spaces; $i++)
        {
            $indent .= " ";
        }
        
        if(substr_count($code, "\n") <= 1)
            return $indent . $code;
        
        $code = str_replace("\n", "\n$indent", $code);
        
        return $code;
    }
1075186
public function prepareTxt($txt, $spaces = true)
    {
        if ($spaces) {
            $txt = preg_replace('/\s+/isu', ' ', $txt);
        }
        $txt = str_replace('&euro;', '€', $txt);
        $txt = html_entity_decode($txt, ENT_QUOTES, $this->encoding);
        return $txt;
    }
579559
public static function concat(iterable $pieces, ?string $delimiter = null): self
    {
        $delimiter = is_null($delimiter) ? Text::EMPTY_TEXT : $delimiter;

        $temp = TextVector::make($pieces)
            ->concat($delimiter);

        return self::make($temp);
    }
496811
protected function replaceLastOne($search, $replace, $subject, $encoding, &$count, &$offset = null, $lenSearch = null)
	{
		$target = mb_substr($subject, 0, $offset, $encoding);
		$position = mb_strrpos($target, $search, 0, $encoding);

		if ($position === false){
			$offset = false;
			return $subject;
		}

		$lenSearch = is_null($lenSearch) ? mb_strlen($search, $encoding) : $lenSearch;

		$start = mb_substr($subject, 0, $position, $encoding);
		$end = $replace.mb_substr($subject, $position+$lenSearch, null, $encoding);

		$offset = -mb_strlen($end, $encoding);
		++$count;

		return $start.$end;
	}
1195701
private function replace(array $data): string
    {
        $widget = explode(':', $data[1]);
        if (class_exists($class = $widget[0]) && method_exists($class, $method = $widget[1])) {
            return call_user_func([$class, $method]);
        }

        return '';
    }
593017
@Override
    public ReplaceDeploymentPlanBuilder andReplace(String toReplace) {
        String newContentKey = getAddedContentKey();
        return replace(newContentKey, toReplace);
    }
1211575
public function splice($replacement, $offset, $length = null) {
		$offset = $this->prepareOffset($offset);
		$length = $this->prepareLength($offset, $length);

		$start = $this->substring(0, $offset);
		$end = $this->substring($offset + $length);

		return new Text($start . $replacement . $end);
	}
1182793
function(piece, quote_char) {
            // Matches everything that is not a quote or \
            var regex = new RegExp("[^" + quote_char + "\\\\]", 'g');
            // Remove eveything that is not quote or \
            var replaced = piece.replace(regex, '');
            // Remove escaped quotes, then remove all remaining slashes, then count.
            return replaced.replace(
            	new RegExp('(\\\\' + quote_char + ')', 'g'), ''
			).replace(
				/\\/g, ''
			).length;
        }
1174276
protected function _implodeRecursive(array $pieces)
    {
        $values = array();
        foreach ($pieces as $item) {
            if (is_array($item)) {
                $values[] = $this->_implodeRecursive($item);
            } else {
                $values[] = $item;
            }
        }

        return implode(', ', $values);
    }
268251
public static function removeUrlSegment($url, $segment, $replacement = '/')
    {
        $preparedSegment = trim($segment, '/');
        if (empty($preparedSegment)) {
            return $url;
        }
        preg_match('/^([^?]+?)(\?.+?)?$/', $url, $matches); // get url and get apart
        return preg_replace(
                '/(?:\/|^)' . $preparedSegment . '(?:\/|$)/i',
                $replacement,
                (isset($matches[1]) ? $matches[1] : '')
            ) . (isset($matches[2]) ? $matches[2] : '');
    }
438263
def _replace_spaces(self, name: str) -> str:
        
        if self.replace_spaces:
            return name.strip().replace(' ', str(self.replace_spaces_with))
        return name
717457
private function isNotEmpty(string $piece): bool
    {

        $isEmpty = is_blank($piece);
        $isDot = ('.' === $piece);

        return !($isEmpty || $isDot);
    }