Jump to content


Photo

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


  • Please log in to reply
2 replies to this topic

#1 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 07 July 2006 - 12:16 PM

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.


regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 07 July 2006 - 12:47 PM

Im pretty sure you can you substr_replace:

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

That would then output 12345678

#3 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 07 July 2006 - 01:21 PM

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.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users