PHP » PYTHON |
login |
register |
about
|
Python 2.x:
import string result = myStr.translate(string.maketrans(from, to)) Python 3.x: result = myStr.translate(str.maketrans(from, to)) result = myStr.translate(str.maketrans(replace_dict)) Caveat: these solutions only cover the first use case of strtr(): plain character substitution. It does not implement the second case: array tokens. strtr(PHP 4, PHP 5) strtr — Translate certain characters Description
string strtr
( string $str
, string $from
, string $to
)
string strtr
( string $str
, array $replace_pairs
)
This function returns a copy of str , translating all occurrences of each character in from to the corresponding character in to . If from and to are different lengths, the extra characters in the longer of the two are ignored. Parameters
Return ValuesThis function returns a copy of str , translating all occurrences of each character in from to the corresponding character in to . Changelog
Examples
Example #1 strtr() example
<?php
strtr() may be called with only two arguments. If called with two arguments it behaves in a new way: from then has to be an array that contains string -> string pairs that will be replaced in the source string. strtr() will always look for the longest possible match first and will *NOT* try to replace stuff that it has already worked on. Example #2 strtr() example with two arguments
<?php The above example will output: hello all, I said hi |
more
Recently updated
more
Most requested
more
Last requests
|