Skip to content

Instantly share code, notes, and snippets.

@stemar
Last active October 24, 2024 09:33
Show Gist options
  • Select an option

  • Save stemar/8287074 to your computer and use it in GitHub Desktop.

Select an option

Save stemar/8287074 to your computer and use it in GitHub Desktop.

Revisions

  1. stemar revised this gist Dec 24, 2022. No changes.
  2. stemar revised this gist May 23, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions mb_substr_replace.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    function mb_substr_replace($string, $replacement, $start, $length=NULL) {
    if (is_array($string)) {
    $num = count($string);
  3. stemar renamed this gist Feb 16, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. stemar renamed this gist Jan 6, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. stemar revised this gist Jan 6, 2014. No changes.
  6. stemar created this gist Jan 6, 2014.
    35 changes: 35 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    function mb_substr_replace($string, $replacement, $start, $length=NULL) {
    if (is_array($string)) {
    $num = count($string);
    // $replacement
    $replacement = is_array($replacement) ? array_slice($replacement, 0, $num) : array_pad(array($replacement), $num, $replacement);
    // $start
    if (is_array($start)) {
    $start = array_slice($start, 0, $num);
    foreach ($start as $key => $value)
    $start[$key] = is_int($value) ? $value : 0;
    }
    else {
    $start = array_pad(array($start), $num, $start);
    }
    // $length
    if (!isset($length)) {
    $length = array_fill(0, $num, 0);
    }
    elseif (is_array($length)) {
    $length = array_slice($length, 0, $num);
    foreach ($length as $key => $value)
    $length[$key] = isset($value) ? (is_int($value) ? $value : $num) : 0;
    }
    else {
    $length = array_pad(array($length), $num, $length);
    }
    // Recursive call
    return array_map(__FUNCTION__, $string, $replacement, $start, $length);
    }
    preg_match_all('/./us', (string)$string, $smatches);
    preg_match_all('/./us', (string)$replacement, $rmatches);
    if ($length === NULL) $length = mb_strlen($string);
    array_splice($smatches[0], $start, $length, $rmatches[0]);
    return join($smatches[0]);
    }