silkfire Posted May 27, 2011 Share Posted May 27, 2011 Hey all, If I have a number like this: 1245 then I want a '.' inserted at position -2: 12.45 how do I do that in the most simple and elegant way? 74342 -> 743.42 Quote Link to comment https://forums.phpfreaks.com/topic/237629-inject-string-at-a-certain-position/ Share on other sites More sharing options...
gristoi Posted May 27, 2011 Share Posted May 27, 2011 $newstring=substr_replace($orig_string, $insert_string, $position, 0); Quote Link to comment https://forums.phpfreaks.com/topic/237629-inject-string-at-a-certain-position/#findComment-1221045 Share on other sites More sharing options...
Maq Posted May 27, 2011 Share Posted May 27, 2011 If you're trying to monetize the number look at - money_format. Quote Link to comment https://forums.phpfreaks.com/topic/237629-inject-string-at-a-certain-position/#findComment-1221111 Share on other sites More sharing options...
Adam Posted May 27, 2011 Share Posted May 27, 2011 $newstring=substr_replace($orig_string, $insert_string, $position, 0); How does that help? Quote Link to comment https://forums.phpfreaks.com/topic/237629-inject-string-at-a-certain-position/#findComment-1221118 Share on other sites More sharing options...
gristoi Posted May 27, 2011 Share Posted May 27, 2011 Sorry, i thought it wouldnt be a great leap for him to follow the syntax: $string = '74342'; $newstring=substr_replace($string, '.', -2, 0); echo $newstring; // result = 743.42 Quote Link to comment https://forums.phpfreaks.com/topic/237629-inject-string-at-a-certain-position/#findComment-1221125 Share on other sites More sharing options...
silkfire Posted May 27, 2011 Author Share Posted May 27, 2011 Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/237629-inject-string-at-a-certain-position/#findComment-1221172 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.