Jump to content

Replacing every * letter from a string


NLT

Recommended Posts

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.