Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8367191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:05:47+00:00 2026-06-09T13:05:47+00:00

By substr_replace , I can insert a string in a specific index: $a =

  • 0

By substr_replace, I can insert a string in a specific index:

$a = 'abcdefg';
echo substr_replace($a, '1', 2, 0); // ab1cdefg

But, Is it possible to insert 2-3 strings at specific positions with just 1 substr_replace? (or other built-in functions)
for example:

$a = 'abcdefg';
$b = array('1', '2', '3');
$c = array(1, 2, 4);
echo substr_replace($a, $b, $c, 0); // a1b2cd3efg

I tried that code, but it returned an error:

Warning: substr_replace() [function.substr-replace]: ‘from’ and ‘len’
should be of same type – numerical or array

Thanks…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:05:49+00:00Added an answer on June 9, 2026 at 1:05 pm

    Now, first off, let me say that from reading the manual, I would have thought your code should work, you seem to be following the rules set out correctly.

    What is even more ridiculous is that if you try and fix it by passing an array of 0s to $len (sounds reasonable based on that error message, no?) you get this:

    Warning: substr_replace(): Functionality of ‘from’ and ‘len’ as arrays is not implemented

    Right. So not only is the manual wrong, even the error messages contradict themselves. God I love PHP.

    But enough of this, on to a solution…


    Because it doesn’t work as expected, you will have to loop in order to do what you want. Not too hard, right? Well, the major tripping hazard here is that the length of the string and the offset positions will change on every iteration. In order to stop this from causing a problem, you will need to perform the replacements in reverse so that the indexes are where you expect them to be.

    Something like this:

    <?php
    
      $a = 'abcdefg';
      $b = array('1', '2', '3');
      $c = array(1, 2, 4);
    
      for ($i = count($c) - 1; $i >= 0; $i--) {
        $a = substr_replace($a, $b[$i], $c[$i], 0);
      }
      echo $a; // a1b2cd3efg
    

    See it working

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a space or other non-printing character I can insert to a varchar2
I try to remove some non-safe characters from a string but i believe i
I want to insert in Different tables with only single FORALL Loop in oracle.but
If I have this string: k6s6k6s6c1u6t661w651z6k6z6z6k611 How can delete all the characters in even-numbers
How can i link the title in this code. echo <div id='vblock'>; echo <div
Possible Duplicate: How to Truncate a string in PHP to the word closest to
I have searched everywhere but can't find a solution that works for me. I
I have two strings, string1 and string2. I want to check if string1 can
I would like to write a javascript plug in which can string manipulations something
i am using substr to trim the first 100 characters from the string. however

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.