/** * Strips comments from primary type, sub type, parameter name or parameter value. * * Also removes leading and trailing whitespace. * * @param string $string The string to strip comments from * @throws \Ableron\Core\Exception\SystemException * @return string */ public static function stripComments($string) { // check whether there are comments within the given string if (StringUtil::getFirstIndexOf($string, '(') === -1) { return trim($string); } // prepare required variables $returnValue = ''; $isEscaped = false; // indicates whether the current character is escaped $inQuotes = false; // indicates whether the current character is located within a quoted string $commentLevel = 0; // indicates the level of the nested comment where the current character is located // process the given string, character by character for ($i = 0, $stringLength = StringUtil::getLength($string); $i < $stringLength; $i++) { // get current character $currentCharacter = StringUtil::getSubstring($string, $i, 1); // check if current character is escaped if ($isEscaped) { // add character to return value if we are not inside a comment if ($commentLevel == 0) { $returnValue .= '\\' . $currentCharacter; } // next character is not escaped $isEscaped = false; // check if we have a backslash escaping the next character } elseif ($currentCharacter == '\\') { $isEscaped = true; // check if current character starts a comment } elseif ($currentCharacter == '(' && !$inQuotes) { $commentLevel++; // check if current character ends a comment } elseif ($currentCharacter == ')' && !$inQuotes && $commentLevel > 0) { $commentLevel--; // check if current character starts/ends a quoted string } elseif ($currentCharacter == '"') { // add character to return value if we are not inside a comment if ($commentLevel == 0) { $returnValue .= $currentCharacter; } $inQuotes = !$inQuotes; // not a special character. simply add to the return value if not located within a comment } elseif ($commentLevel == 0) { $returnValue .= $currentCharacter; } } // check for valid syntax if ($inQuotes) { throw new SystemException(sprintf('Unable to strip comments from "%s" - String contains invalid quotation mark!', $string), 0, E_USER_NOTICE, __FILE__, __LINE__); } elseif ($commentLevel != 0) { throw new SystemException(sprintf('Unable to strip comments from "%s" - Comment not closed!', $string), 0, E_USER_NOTICE, __FILE__, __LINE__); } // return the string with comments stripped return trim($returnValue); }
/** * Removes dot segments (/./, /../) from the given path. * * @param string $path The path to remove the dot-segments from * @return string */ public static function removeDotSegments($path) { // check whether path may contain dot segments if (!preg_match('#(^\\.\\.?$|/\\.|\\./)#', $path)) { return $path; } $pathParts = array(); $removeLastSegment = false; while ($path !== '') { switch (true) { case $path === '.' || $path === '..': break 2; case $path === '/.': $path = '/'; break; case $path === '/..': $path = '/'; $removeLastSegment = true; break; case StringUtil::startsWith($path, '/../'): $path = StringUtil::getSubstring($path, 3); $removeLastSegment = true; break; case StringUtil::startsWith($path, './') || StringUtil::startsWith($path, '/./'): $path = StringUtil::getSubstring($path, 2); break; case StringUtil::startsWith($path, '../'): $path = StringUtil::getSubstring($path, 3); break; default: $isLastSegment = ($nextSegmentStart = StringUtil::getFirstIndexOf($path, '/', 1)) === -1; $currentSegment = $isLastSegment ? $path : StringUtil::getSubstring($path, 0, $nextSegmentStart); array_push($pathParts, $currentSegment); $path = $isLastSegment ? '' : StringUtil::getSubstring($path, StringUtil::getLength($currentSegment)); } if ($removeLastSegment) { $removeLastSegment = false; if (!empty($pathParts)) { array_pop($pathParts); } } } return implode('', $pathParts); }