Advertisement
retesere20

php - mb_substr_replace

Nov 27th, 2019
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = "UTF-8") {
  2. if (extension_loaded('mbstring') === true){
  3. $string_length = (is_null($encoding) === true) ? mb_strlen($string) : mb_strlen($string, $encoding);
  4. if ($start < 0) { $start = max(0, $string_length + $start); }
  5. else if ($start > $string_length) {$start = $string_length; }
  6. if ($length < 0){ $length = max(0, $string_length - $start + $length); }
  7. else if ((is_null($length) === true) || ($length > $string_length)) { $length = $string_length; }
  8. if (($start + $length) > $string_length){$length = $string_length - $start;}
  9. if (is_null($encoding) === true) { return mb_substr($string, 0, $start) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length); }
  10. return mb_substr($string, 0, $start, $encoding) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length, $encoding);
  11. }
  12. return (is_null($length) === true) ? substr_replace($string, $replacement, $start) : substr_replace($string, $replacement, $start, $length);
  13. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement