1
0
Fork 0
forked from mirror/pixelfed
pixelfed/app/Util/Lexer/StringUtils.php

108 lines
3.1 KiB
PHP
Raw Normal View History

2018-06-09 03:31:42 +00:00
<?php
/**
* @author Takashi Nojima
* @copyright Copyright 2014, Takashi Nojima
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
*/
namespace App\Util\Lexer;
/**
2018-08-28 03:07:36 +00:00
* String utility.
2018-06-09 03:31:42 +00:00
*
* @author Takashi Nojima
* @copyright Copyright 2014, Takashi Nojima
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
*/
class StringUtils
{
/**
2018-08-28 03:07:36 +00:00
* alias of mb_substr.
2018-06-09 03:31:42 +00:00
*
* @param string $str
2018-08-28 03:07:36 +00:00
* @param int $start
* @param int $length
2018-06-09 03:31:42 +00:00
* @param string $encoding
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @return string
*/
public static function substr($str, $start, $length = null, $encoding = 'UTF-8')
{
if (is_null($length)) {
// for PHP <= 5.4.7
$length = mb_strlen($str, $encoding);
}
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return mb_substr($str, $start, $length, $encoding);
}
/**
2018-08-28 03:07:36 +00:00
* alias of mb_strlen.
2018-06-09 03:31:42 +00:00
*
* @param string $str
* @param string $encoding
2018-08-28 03:07:36 +00:00
*
* @return int
2018-06-09 03:31:42 +00:00
*/
public static function strlen($str, $encoding = 'UTF-8')
{
return mb_strlen($str, $encoding);
}
/**
2018-08-28 03:07:36 +00:00
* alias of mb_strpos.
2018-06-09 03:31:42 +00:00
*
* @param string $haystack
* @param string $needle
2018-08-28 03:07:36 +00:00
* @param int $offset
2018-06-09 03:31:42 +00:00
* @param string $encoding
2018-08-28 03:07:36 +00:00
*
* @return int
2018-06-09 03:31:42 +00:00
*/
public static function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8')
{
return mb_strpos($haystack, $needle, $offset, $encoding);
}
/**
* A multibyte-aware substring replacement function.
*
2018-08-28 03:07:36 +00:00
* @param string $string The string to modify.
* @param string $replacement The replacement string.
* @param int $start The start of the replacement.
* @param int $length The number of characters to replace.
* @param string $encoding The encoding of the string.
2018-06-09 03:31:42 +00:00
*
2018-08-28 03:07:36 +00:00
* @return string The modified string.
2018-06-09 03:31:42 +00:00
*
* @see http://www.php.net/manual/en/function.substr-replace.php#90146
*/
public static function substrReplace($string, $replacement, $start, $length = null, $encoding = 'UTF-8')
{
if (extension_loaded('mbstring') === true) {
$string_length = static::strlen($string, $encoding);
if ($start < 0) {
$start = max(0, $string_length + $start);
} elseif ($start > $string_length) {
$start = $string_length;
}
if ($length < 0) {
$length = max(0, $string_length - $start + $length);
} elseif ((is_null($length) === true) || ($length > $string_length)) {
$length = $string_length;
}
if (($start + $length) > $string_length) {
$length = $string_length - $start;
}
$suffixOffset = $start + $length;
$suffixLength = $string_length - $start - $length;
2018-08-28 03:07:36 +00:00
return static::substr($string, 0, $start, $encoding).$replacement.static::substr($string, $suffixOffset, $suffixLength, $encoding);
2018-06-09 03:31:42 +00:00
}
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return (is_null($length) === true) ? substr_replace($string, $replacement, $start) : substr_replace($string, $replacement, $start, $length);
}
}