DmitryDulepov\Realurl\Utility::convertToSafeString PHP Method

convertToSafeString() public method

The result is not url-encoded.
public convertToSafeString ( string $processedTitle, string $spaceCharacter = '-', boolean $strToLower = true ) : string
$processedTitle string
$spaceCharacter string
$strToLower boolean
return string
    public function convertToSafeString($processedTitle, $spaceCharacter = '-', $strToLower = true)
    {
        if ($strToLower) {
            $processedTitle = $this->csConvertor->conv_case('utf-8', $processedTitle, 'toLower');
        }
        $processedTitle = strip_tags($processedTitle);
        $processedTitle = preg_replace('/[ \\t\\x{00A0}\\-+_]+/u', $spaceCharacter, $processedTitle);
        $processedTitle = $this->csConvertor->specCharsToASCII('utf-8', $processedTitle);
        $processedTitle = preg_replace('/[^\\p{L}0-9' . preg_quote($spaceCharacter) . ']/u', '', $processedTitle);
        $processedTitle = preg_replace('/' . preg_quote($spaceCharacter) . '{2,}/', $spaceCharacter, $processedTitle);
        $processedTitle = trim($processedTitle, $spaceCharacter);
        // TODO Post-processing hook here
        if ($strToLower) {
            $processedTitle = strtolower($processedTitle);
        }
        return $processedTitle;
    }

Usage Example

Example #1
0
 /**
  * Test if whitespace is trimmed and spaces are treated the same as tabs and nbsp
  *
  * @test
  * @see https://github.com/dmitryd/typo3-realurl/issues/218
  */
 public function convertToSafeStringWithWhitespace()
 {
     // the string should be trimmed
     $this->assertEquals('trim', $this->utility->convertToSafeString("  trim  "));
     // the next line contains a non-breaking-sapce (\x20)
     $this->assertEquals('non-breaking-space-split', $this->utility->convertToSafeString("non-breaking-space split"), 'Non-breaking-spaces should be treated as whitespace');
     // tabs should be treated the same as white-space
     $this->assertEquals('tab-split', $this->utility->convertToSafeString("tab\tsplit"), 'tabs should be treated the same as white-space');
 }