-
Notifications
You must be signed in to change notification settings - Fork 4
/
strtr.php
76 lines (63 loc) · 1.77 KB
/
strtr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
// strtr
$str = "Hello World!";
print strlen($str) . "\n";
// string(12) "****o Wor*d!"
// string(12) "*ello World!"
// string(12) "Hello World!"
// string(12) "***** W*r*d!"
// string(12) "Hello World!"
// string(12) "Hello World!"
var_dump( strtr($str, "Hello", "****") );
var_dump( strtr($str, "Hello", "*") );
var_dump( strtr($str, "Hello", "") );
var_dump( strtr($str, "Hello", "*********") );
var_dump( strtr($str, "Hello", "Hello") );
var_dump( strtr($str, "Hello", "Hello World!") );
// string(12) "Hello Worl*!"
// string(12) "Hello Worl*!"
var_dump( strtr($str, "abcde", "****") );
var_dump( strtr($str, "abcd", "*****") );
// string(12) "****o Wor*d!"
var_dump( strtr($str, "Hello", "**e*") );
// string(12) "**eeo Wored!"
var_dump( strtr($str, "Hello", "***e") );
// $action = [
// "a" => "!",
// "b" => "@",
// "c" => "#"
// ];
// var_dump( strtr($str, $action) );
$pairs = [
"h" => "-",
"hello" => "hi",
"hi" => "hello"
];
// hello all, I said hi
// hello all, I said hi
// hello all, I said hi -
// hello all, I said hi - -o
echo strtr("hi all, I said hello h ho", $pairs);
// The two modes of behavior are substantially different.
// With three arguments, strtr() will replace bytes; with two,
// it may replace longer substrings.
print "\n";
$pairs = [
"h" => "-",
"hilow" => "SSSSS",
"hi" => "hello"
];
echo strtr("hi all, I said hello h ho", $pairs);
// hello all, I said -ello - -o
print "\n";
echo strtr("hi all, I said hilow hiloX h ho", $pairs);
// hello all, I said SSSSS - -o
print "\n===================\n";
$dict = [
"hello" => "سلام",
"bye" => "خداحافظ",
"how are you" => "چطوری؟",
"hey" => "هوی!"
];
echo strtr("hello bye how are you hey", $dict);
// سلام خداحافظ چطوری؟ هوی!