A character translation utility similar to the UNIX tr command. A transliteration consists of a pattern of characters to match and a pattern, typically of equal length, of characters to replace each match with. The tr utility loops through a specified string character by character. Each character is compared against the pattern of matching characters. If the character matches one of the pattern characters it is either replaced with the replacement character mapped to that matcher or it is deleted. Which action is taken depends on what options are specified.
"string" - the string to apply this translation function to.
"matcher" - the characters that will be swapped out. Ranges will be expanded to all of the characters. A range is specified as n-m where n is the starting character (A-Z, a-z, 0-9) and m is the ending character. Backwards ranges are allowed as well. The matcher pattern may also contain the following character classes:
Sequence Meaning . Matches any character \d Matches any digit 0-9 \D Matches any non-digit \s Matches any whitespace character \S Matches any non-whitespace character \w Matches any word character (a-z, A-Z, _, and 0-9) \W Matches any non-word character \\ Matches a literal backslash \. Matches a literal period \- Matches a literal dash
"replacement" - a string of replacement characters that maps 1:1 (after expansion) to the "matcher" string. As with the "matcher" string, ranges are expanded here as well.
'options' - A combination of the following parameters that alters the behavior of the matcher/translator.
Sequence Name Description c complement negates matcher pattern forcing matcher chars/patterns to match their complement. d delete delete all characters with no mapping in the replacement pattern. s squeeze squeeze together matches that occur next to eachother. Deletes repeated matches.
The transliterated string.
# A simple ROT13 Translator (for extra security run it twice...) $cipher = tr("sleep rocks", "a-z", "n-za-m"); $plain = tr($cipher, "a-z", "n-za-m"); println("Cipher: $cipher Plain: $plain");
Cipher: fyrrc ebpxf Plain: sleep rocks
# remove all duplicate chars from $string $string = "thhisss iiiisss mmmy sssstrriinngg"; $string = tr($string, "a-zA-Z0-9 ", "a-zA-Z0-9 ", "sd"); println($string);
this is my string