strtr 


string apg

TRANSLATES characters or replace substrings.




Considering $n a valid offset, every occurrence of $from[$n] will be repalced by $to[$n].

If $from and $to have different lengths, the extra characters in the longer of the two are ignored.

The length of $str will be the same as the return value's.

This function return the translated $str.

The $replace_pairs may be used instead of $to and $from, in which case it's an array in the form ['from' => 'to', ...].

If $replace_pairs contains a key which is an empty STRING, FALSE will be returned.

If the $str is not a scalar then it is not typecasted into a string, instead a WARNING is raised and NULL is returned.




<?php

str strtr 
( str $str , str $from , str $to )


where,

$str = The STRING to be tanslated

$from 
= The STRING to be tanslated to $to

$to 
= The STRING to repalace $from

?>

$str


The STRING to be translated.



$from


The STRING being translated to $to.



$to


The STRING replacing $from.



<?php

str strtr 
( str $str , arr $replace_pairs )


where,

$str = The STRING to be tanslated

$replace_pairs 
= To be used instead $to and $from

?>

$str


The STRING to be translated.



$replace_pairs


To be used instead $to and $from.



  1 EXERCISE   

<?php

$strt01 
= 'AAABBBAAABBBBB';

echo 
$strt01 . '<br><br>';

$str_f01a = 'AB';

$srt_f01b = '01';

$str_end01 = strtr($strt01, $str_f01a, $srt_f01b);

echo 
$str_end01; 

?>

 RESULT   

STRING BASE

AAABBBAAABBBBB

translating AB by 01

where,

A = 0 and B = 1

STRING RESULT

00011100011111


  2 EXERCISE   

<?php

$strt02 
= 'Whoever laughs last, GENERALLY, laughs best!';

echo 
$strt02 . '<br><br>';

$arr_f02 = ["GENERALLY" => "NOT ALWAYS"];

$str_end02 = strtr($strt02, $arr_f02);

echo 
$str_end02; 

?>

 RESULT   

Whoever laughs last, GENERALLY, laughs best!

Whoever laughs last, NOT ALWAYS, laughs best!


  3 EXERCISE   

<?php

$strt03 
= 'Whoever laughs last, GENERALLY, laughs best!';

echo 
$strt03 . '<br><br>';

$from03 = 'abcdefghstluovr';

$to03 = 'ABCDEFGHSTLUOVR';

$str_end03 = strtr($strt03, $from03, $to03);

echo 
$str_end03; 

?>

 RESULT   

Whoever laughs last, GENERALLY, laughs best!

WHOEVER LAUGHS LAST, GENERALLY, LAUGHS BEST!


  4 EXERCISE   

<?php

$trans 
= ["hello"=>"hi", 
                    
"hi"=>"hello", 
                     
"a"=>"A", 
              
"world"=>"planet"];

var_dump(strtr("# hi all, I said hello world! #", $trans));

?>

  5 EXERCISE   

<?php

echo "Testing strtr() : basic functionality.<br><br>";

//definitions of required input variables
$trans1_arr = ["t" => "T", "e" => "E", "st" => "ST"];

$trans2_arr = ['t' => 'T', 'e' => 'E', 'st' => 'ST'];

$heredoc_str = <<<EOD
Test strtr
EOD;

echo 
'Provided elements:<br><br>';
print_r($trans1_arr);
echo 
'<br><br>';
print_r($trans2_arr);
echo 
'<br><br>';
echo 
$heredoc_str;
echo 
'<br><br><br>Results:<br><br>';
//translating single char
var_dump( strtr("test strtr", "t", "T") );
echo 
'<br><br>';
var_dump( strtr('test strtr', 't', 'T') );
echo 
'<br><br>';
var_dump( strtr($heredoc_str, "t", "T") );
echo 
'<br><br><br>';
//translating set of chars
//$from and $to are of same length
var_dump( strtr("test strtr", "test", "TEST") );
echo 
'<br><br>';
var_dump( strtr('test strtr', 'test', 'TEST') );
echo 
'<br><br>';
var_dump( strtr($heredoc_str, "test", "TEST") );
echo 
'<br><br><br>';
// $from and $to are of different lengths, 
// extra chars in the longer one are ignored
var_dump( strtr("test strtr", "test", "TESTz") );
echo 
'<br><br>';
var_dump( strtr('test strtr', 'testz', 'TEST') );
echo 
'<br><br>';
var_dump( strtr($heredoc_str, "test", "TESTz") );
echo 
'<br><br><br>';
//by using replace_pairs array
var_dump( strtr("test strtr", $trans1_arr) );
echo 
'<br><br>';
var_dump( strtr('test strtr', $trans2_arr) );
echo 
'<br><br>';
var_dump( strtr($heredoc_str, $trans1_arr) );

?>

  6 EXERCISE   

<?php

var_dump
(strtr("foo", ["" => "bar"]));
echo 
'<br><br>';
var_dump(strtr("foo", ["" => "bar", "x" => "y"]));

?>

  7 EXERCISE   

<?php

/* Testing strtr() function by 
 * assing the combination of numeric 
 * & regular strings for 'str' argument and
 * corresponding translation pair of chars
 * for 'from', 'to' & 'replace_pairs' 
 * arguments
*/

echo "Testing strtr() : 
         numeric & regular double quoted strings.<br>"
;
/* definitions of required input variables */
$count = 1;

$heredoc_str = <<<EOD
123
abc
1a2b3c
EOD;

//array of string inputs for $str
$str_arr = [
  
//double quoted strings
  
"123",
  
"abc",
  
"1a2b3c",

  
//single quoted strings
  
'123',
  
'abc',
  
'1a2b3c',

  
//heredoc string
  
$heredoc_str
];

$from = "123abc";

$to = "abc123";
$replace_pairs = ["1" => "a", "a" => 1, 
                      
"2b3c" => "b2c3", 
                      
"b2c3" => "3c2b"];

/* loop through to test strtr() 
           with each element of $str_arr */
for($index = 0; $index < count($str_arr); $index++) {
  echo 
"<br><br>Iteration: $count<br>";

  
$str = $str_arr[$index];  
  
//getting the $str_arr element in $str variable

  //strtr() call in three args syntax form
  
var_dump( strtr($str, $from, $to) );
  echo 
'<br>';
  
//strtr() call in two args syntax form
  
var_dump( strtr($str, $replace_pairs) );

  
$count++;
}

?>

  8 EXERCISE   

<?php

/* Testing strtr() function by passing the
 * string containing various special characters 
 * for 'str' argument and corresponding translation
 *  pair of chars for 'from', 'to' 
 * & 'replace_pairs' arguments
*/

echo "Testing strtr() : 
               string containing special chars 
                              for 'str' arg.<br>"
;

/* definitions of required input variables */
$count = 1;

$heredoc_str = <<<EOD
%
#$*&
text & @()
EOD;

//array of string inputs for $str
$str_arr = array(
  
//double quoted strings
  
"%",
  
"#$*",
  
"text & @()",

  
//single quoted strings
  
'%',
  
'#$*',
  
'text & @()',

  
//heredoc string
  
$heredoc_str
);

$from = "%#$*&@()";
$to = "specials";

$replace_pairs = array("$" => "%", 
                                    
"%" => "$", 
                           
"#*&@()" => "()@&*#");

/* loop through to test strtr() 
             with each element of $str_arr */
for($index = 0; $index < count($str_arr); $index++) {
  echo 
"<br><br>Iteration: $count<br>";

  
$str = $str_arr[$index];  
  
//getting the array element in 'str' variable

  //strtr() call in three args syntax form
  
var_dump( strtr($str, $from, $to) );

  
//strtr() call in two args syntax form
  
var_dump( strtr($str, $replace_pairs) );

  
$count++;
}

?>

  9 EXERCISE   

<?php

/* Testing strtr() function by passing the
 * string containing various escape sequences 
 * for 'str' argument and corresponding 
 * translation pair of chars for 'from', 'to' 
 * & 'replace_pairs' arguments
*/

echo "Testing strtr() : 
             string containing escape sequences 
                                  for 'str' arg.<br>"
;
/* definitions of required input variables */
$count = 1;

$heredoc_str = <<<EOD
\tes\t\\stt\r
\\test\\\strtr
\ntest\r\nstrtr
\$variable
\"quotes
EOD;

//array of string inputs for $str
$str_arr = [
  
//double quoted strings
  
"\tes\t\\stt\r",
  
"\\test\\\strtr",
  
"\ntest\r\nstrtr",
  
"\$variable",
  
"\"quotes",

  
//single quoted strings
  
'\tes\t\\stt\r',
  
'\\test\\\strtr',
  
'\ntest\r\nstrtr',
  
'\$variable',
  
'\"quotes',

  
//heredoc string
  
$heredoc_str
];

$from = "\n\r\t\\";
$to = "TEST";
$replace_pairs = ["\n" => "t", 
                 
"\r\n" => "T", 
                 
"\n\r\t\\" => "TEST"];

/* loop through to test strtr() 
              with each element of $str_arr */
for($index = 0; $index < count($str_arr); $index++) {
  echo 
"<br><br>Iteration: $count<br>";

  
$str = $str_arr[$index];
  
//getting the array element in 'str' variable

  //strtr() call in three args syntax form
  
var_dump( strtr($str, $from, $to) );
  echo 
"<br>";
  
//strtr() call in two args syntax form
  
var_dump( strtr($str, $replace_pairs) );

  
$count++;
}

?>

  10 EXERCISE   

<?php

/* Testing strtr() function by passing the
 * empty string & null for 'str' argument and
 * corresponding translation pair of chars for
 * 'from', 'to' & 'replace_pairs' arguments
*/

echo "Testing strtr() : 
               empty string & null for 'str' arg.<br>"
;
/* definitions of required input variables */
$count = 1;

$heredoc_str = <<<EOD

EOD;

//array of string inputs for $str
$str_arr = array(
  
"",
  
'',
  
NULL,
  
null,
  
FALSE,
  
false,
  
$heredoc_str
);

$from = "";
$to = "TEST";
$replace_pairs = array("" => "t", '' => "TEST");


/* loop through to test strtr() 
           with each element of $str_arr */
for($index = 0; $index < count($str_arr); $index++) {
  echo 
"<br><br>Iteration: $count<br>";

  
$str = $str_arr[$index];
  
//getting the array element in 'str' variable

  //strtr() call in three args syntax form
  
var_dump( strtr($str, $from, $to) );
  echo 
"<br>";
  
//strtr() call in two args syntax form
  
var_dump( strtr($str, $replace_pairs) );

  
$count++;
}

?>

  11 EXERCISE   

<?php

/* Test strtr() function: 
 * with unexpected inputs for 'from'
 * and expected type for 'str' & 'to' arguments
*/

echo "Testing strtr() function: 
               with unexpected inputs for 'from'.<br>"
;

//get an unset variable
$unset_var = 'string_val';
unset(
$unset_var);

//defining a class
class sample  {
  public function 
__toString() {
    return 
"sample object";
  }
}

//getting the resource
$file_handle = fopen(__FILE__, "r");

//defining 'str' argument
$str = "012atm";

// array of values for 'from'
$from_arr =  array (

          
// integer values
/*1*/      
0,
          
1,
          -
2,

          
// float values
/*4*/      
10.5,
          -
20.5,
          
10.1234567e10,

          
// array values
/*7*/      
array(),
          array(
0),
          array(
1, 2),

          
// boolean values
/*10*/      
true,
          
false,
          
TRUE,
          
FALSE,

          
// null values
/*14*/      
NULL,
          
null,

          
// objects
/*16*/      
new sample(),

          
// resource
/*17*/      
$file_handle,

          
// undefined variable
/*18*/      
@$undefined_var,

          
// unset variable
/*19*/      
@$unset_var
);

//defining 'to' argument
$to = "atm012";

// loop through with each element of the 
// $from array to test strtr() function
$count = 1;
for(
$index = 0; $index < count($from_arr); $index++) {
  echo 
"<br><br>Iteration: $count<br>";
  
$from = $from_arr[$index];
  try {
    
var_dump(strtr($str, $from, $to));
  } catch (
TypeError $exception) {
    echo 
$exception->getMessage() . "<br>";
  }
  
$count++;
}

fclose($file_handle);
//closing the file handle

?>

  12 EXERCISE   

<?php

/* Test strtr() function: 
 * with unexpected inputs for 'replace_pairs'
 *  and expected type for 'str' arguments
*/

echo "Testing strtr() function: 
          with unexpected inputs 
                   for 'replace_pairs'.<br>"
;

//get an unset variable
$unset_var = 'string_val';
unset(
$unset_var);

//defining a class
class sample  {
  public function 
__toString() {
    return 
"sample object";
  }
}

//getting the resource
$file_handle = fopen(__FILE__, "r");

//defining 'str' argument
$str = "012atm";

// array of inputs for 'replace_pairs' argument
$replace_pairs_arr =  array (

  
// integer values
  
0,
  
1,
  -
2,

  
// float values
  
10.5,
  -
20.5,
  
10.5e10,

  
// array values
  
array(),
  array(
0),
  array(
1, 2),

  
// boolean values
  
true,
  
false,
  
TRUE,
  
FALSE,

  
// null values
  
NULL,
  
null,

  
// objects
  
new sample(),

  
// resource
  
$file_handle,

  
// undefined variable
  
@$undefined_var,

  
// unset variable
  
@$unset_var
);

// loop through with each element of the 
// $replace_pairs array to test strtr() function
$count = 1;
for(
$index = 0; 
                
$index < count($replace_pairs_arr); 
                
$index++) {
    echo 
"<br><br>Iteration: $count<br>";
    
$replace_pairs = $replace_pairs_arr[$index];
    try {
        
var_dump(strtr($str, $replace_pairs));
    } catch (
TypeError $e) {
        echo 
$e->getMessage() . "<br>";
    }

    
$count ++;
}

fclose($file_handle);  
//closing the file handle

?>

  13 EXERCISE   

<?php

$foo 
= 'foo';
$arr = ['bar' => &$foo];
var_dump(strtr('foobar', $arr));

?>