note 109820 deleted from function.strtr by cmb
| From: | cmb@php.net | Date: | Sun, 29 Dec 2019 10:02:09 +0000 |
| Subject: | note 109820 deleted from function.strtr by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213653@lists.php.net to get a copy of this message | ||
Note Submitter: patrick dot rauchfuss at gmail dot com
----
Here my solution of an classes recursive caseinsentive strtr..
<?php
class String
{
public static function stritr(&$string, $from, $to = NULL)
{
if(is_string($from))
$string = preg_replace("'$from'i", $to, $string);
else if(is_array($from))
{
foreach ($from as $key => $val)
self::stritr($string, $key, $val);
}
return $string;
}
}
// example:
$string = "Hello world. This is just a simple test";
print String::stritr($string, 'WorLd', 'foo');
// array example:
print String::stritr($string, array('WorLd' => 'foo', 'TEST' =>
'bar'));
?>