-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathReplaceUtils.php
49 lines (45 loc) · 1.74 KB
/
ReplaceUtils.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
<?php
namespace WebSharks\CometCache\Pro\Traits\Shared;
use WebSharks\CometCache\Pro\Classes;
trait ReplaceUtils
{
/**
* String replace ONE time.
*
* @since 150422 Rewrite.
*
* @param string $needle A string to search/replace.
* @param string $replace What to replace `$needle` with.
* @param string $haystack The string/haystack to search in.
* @param bool $caSe_insensitive Defaults to a `FALSE` value.
* Pass this as `TRUE` to a caSe-insensitive search/replace.
*
* @return string The `$haystack`, with `$needle` replaced with `$replace` ONE time only.
*/
public function strReplaceOnce($needle, $replace, $haystack, $caSe_insensitive = false)
{
$needle = (string) $needle;
$replace = (string) $replace;
$haystack = (string) $haystack;
$caSe_mb_strpos = $caSe_insensitive ? 'mb_stripos' : 'mb_strpos';
if (($needle_strpos = $caSe_mb_strpos($haystack, $needle)) === false) {
return $haystack; // Nothing to replace.
}
return (string) substr_replace($haystack, $replace, $needle_strpos, mb_strlen($needle));
}
/**
* String replace ONE time (caSe-insensitive).
*
* @since 150422 Rewrite.
*
* @param string $needle A string to search/replace.
* @param string $replace What to replace `$needle` with.
* @param string $haystack The string/haystack to search in.
*
* @return string The `$haystack`, with `$needle` replaced with `$replace` ONE time only.
*/
public function strIreplaceOnce($needle, $replace, $haystack)
{
return $this->strReplaceOnce($needle, $replace, $haystack, true);
}
}