Jump to content

Archived

This topic is now archived and is closed to further replies.

play_

Inserting characters in the a certain position in a string (not array)

Recommended Posts

I need to insert some characters at a certain position in a string.

for example:

$str = "12345678";

so at position 8, i would want to insert a '9'.

i've tried str_pad() but it isn't doing the job.
and substr_replace() would work fine IF it didn't actually replace the other charatcers, just inserted them in there.


ps: i just noticed how the title of this thread doesn't make much sense. sorry about that. it's 8:30am and i have yet to sleep.

Share this post


Link to post
Share on other sites
Im pretty sure you can you substr_replace:

$var = "1234678";
echo substr_replace($var,'5',4,0);

That would then output 12345678

Share this post


Link to post
Share on other sites
Thanks ginger.
I Just actually went to read the manual (function by function in the 'strings' category) until i reached chunk_split() and it seems to work fine so far.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.