NLT Posted May 5, 2012 Share Posted May 5, 2012 I want to be able to replace, let's say, every fifth letter from a string. The string is unknown, though, it could come from database, or a POST form. So I'm unsure if I could use str_replace. I think I may need to use preg_replace(), but I'm not not sure. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 5, 2012 Share Posted May 5, 2012 No, you could not solve that using just str_replace(). You *may* be able to use preg_replace(). But, you need to clarify your requirement. Are yu wanting to replace every fifth LETTER or every fifth CHARACTER? There is a bug difference. The former is definitely more difficult. You can accomplish the latter with $replaced = preg_replace("#(.{4}).#", '${1}X', $input); Quote Link to comment Share on other sites More sharing options...
NLT Posted May 5, 2012 Author Share Posted May 5, 2012 Just the fifth number. I tried the code you gave me but it replaced EVERY fifth, I just want the fifth only. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 5, 2012 Share Posted May 5, 2012 I tried the code you gave me but it replaced EVERY fifth, I just want the fifth only. Yeah, that's right. The code I provided replaces EVERY fifth letter because that is EXACTLY what you requested. I want to be able to replace, let's say, every fifth letter from a string. I guess I was supposed to assume you wanted something different than what you asked for? If this is not what you want I hope someone else is willing to help you because I won't $newstring = substr_replace($input, 'X', 4, 0); Quote Link to comment Share on other sites More sharing options...
Barand Posted May 5, 2012 Share Posted May 5, 2012 $str = 'abcdefg'; $str[4] = '*'; echo $str; // --> abcd*fg Quote Link to comment Share on other sites More sharing options...
NLT Posted May 5, 2012 Author Share Posted May 5, 2012 I tried the code you gave me but it replaced EVERY fifth, I just want the fifth only. Yeah, that's right. The code I provided replaces EVERY fifth letter because that is EXACTLY what you requested. I want to be able to replace, let's say, every fifth letter from a string. I guess I was supposed to assume you wanted something different than what you asked for? If this is not what you want I hope someone else is willing to help you because I won't $newstring = substr_replace($input, 'X', 4, 0); Thanks, I'm sorry for wording the thread wrong.. was that attitude necessary? Quote Link to comment Share on other sites More sharing options...
xyph Posted May 6, 2012 Share Posted May 6, 2012 We're volunteers, most of us professionals, giving our free time to help beginners 'cause we were all there once. If you want to ask for help, it's a good idea to be accurate in, think about, and proof read your questions/responses. When you don't care enough about your question to ask it properly, we feel you've wasted our time. Even in your reply, you still didn't realize he had given you exactly what you'd asked for. Quote Link to comment Share on other sites More sharing options...
haku Posted May 6, 2012 Share Posted May 6, 2012 The reply was maybe a little harsh. But that said, he gave you what you asked for, and you didn't even say thanks. Quote Link to comment Share on other sites More sharing options...
NLT Posted May 6, 2012 Author Share Posted May 6, 2012 The reply was maybe a little harsh. But that said, he gave you what you asked for, and you didn't even say thanks. I thought I worded the thread right until it got pointed out I got gave what I ask for, and I apologized for that. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 6, 2012 Share Posted May 6, 2012 When you've all cooled down, did my reply help? Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 6, 2012 Share Posted May 6, 2012 I'd do it Barand's way, seems a lot less verbose. Quote Link to comment Share on other sites More sharing options...
NLT Posted May 7, 2012 Author Share Posted May 7, 2012 When you've all cooled down, did my reply help? Yeah, sorry for not mentioning, you probably can see why though. Much appreciated. Quote Link to comment 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.