Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/include/utf8/str_ireplace.php
blob: 7257b0a068e21d651462155cf3692d6f2a108aef (plain)
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
<?php

/**
* @version $Id: str_ireplace.php,v 1.2 2007/08/12 01:20:46 harryf Exp $
* @package utf8
* @subpackage strings
*/

/**
* UTF-8 aware alternative to str_ireplace
* Case-insensitive version of str_replace
* Note: requires utf8_strtolower
* Note: it's not fast and gets slower if $search / $replace is array
* Notes: it's based on the assumption that the lower and uppercase
* versions of a UTF-8 character will have the same length in bytes
* which is currently true given the hash table to strtolower
* @param string
* @return string
* @see http://www.php.net/str_ireplace
* @see utf8_strtolower
* @package utf8
* @subpackage strings
*/
function utf8_ireplace($search, $replace, $str, $count=null)
{
	if (!is_array($search))
	{
		$slen = strlen($search);

		if ($slen == 0)
			return $str;

		$lendif = strlen($replace) - strlen($search);
		$search = utf8_strtolower($search);

		$search = preg_quote($search);
		$lstr = utf8_strtolower($str);
		$i = 0;
		$matched = 0;

		while (preg_match('/(.*)'.$search.'/Us', $lstr, $matches))
		{
			if ($i === $count)
				break;

			$mlen = strlen($matches[0]);
			$lstr = substr($lstr, $mlen);
			$str = substr_replace($str, $replace, $matched+strlen($matches[1]), $slen);
			$matched += $mlen + $lendif;
			$i++;
		}

		return $str;
	}
	else
	{
		foreach (array_keys($search) as $k)
		{
			if (is_array($replace))
			{
				if (array_key_exists($k, $replace))
					$str = utf8_ireplace($search[$k], $replace[$k], $str, $count);
				else
					$str = utf8_ireplace($search[$k], '', $str, $count);
			}
			else
				$str = utf8_ireplace($search[$k], $replace, $str, $count);
		}

		return $str;
	}
}