-
Notifications
You must be signed in to change notification settings - Fork 1
/
Text.php
50 lines (41 loc) · 1.48 KB
/
Text.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
<?php
namespace Onvardgmbh\HelperClasses;
class Text {
/**
* Unicode safe version of substr_replace
*
* @since 0.0.1
*
* @link http://php.net/manual/de/function.substr-replace.php Taken from comment in PHP docs
*
* @param String $string
*
* @return String
*/
public static function mb_substr_replace( $string, $replacement, $start, $length = null, $encoding = null ) {
if ( extension_loaded( 'mbstring' ) === true ) {
$string_length = ( is_null( $encoding ) === true ) ? mb_strlen( $string ) : mb_strlen( $string, $encoding );
if ( $start < 0 ) {
$start = max( 0, $string_length + $start );
} else if ( $start > $string_length ) {
$start = $string_length;
}
if ( $length < 0 ) {
$length = max( 0, $string_length - $start + $length );
} else if ( ( is_null( $length ) === true ) || ( $length > $string_length ) ) {
$length = $string_length;
}
if ( ( $start + $length ) > $string_length ) {
$length = $string_length - $start;
}
if ( is_null( $encoding ) === true ) {
return mb_substr( $string, 0, $start ) . $replacement . mb_substr( $string, $start + $length,
$string_length - $start - $length );
}
return mb_substr( $string, 0, $start, $encoding ) . $replacement . mb_substr( $string, $start + $length,
$string_length - $start - $length, $encoding );
}
return ( is_null( $length ) === true ) ? substr_replace( $string, $replacement,
$start ) : substr_replace( $string, $replacement, $start, $length );
}
}