Jump to content

"Always Progressive" str_replace?


poirot

Recommended Posts

Here is my problem folks:

Sometimes when you need to do simple search and replace you can use str_replace. But let's say you created an encryption scheme, which consists in simple character swapping (like a->8, b->X, and so on). Obviously if you use str_replace it's not going to work, since it will seek the entire string everytime it's called, so some characters could be swapped twice, giving you a bad result.

So, I need something that won't "scan" something that was aleady replaced. I could use a complex code with preg_match , capturing the offsets, and using substr(), but this is kind of annoying. I also tried strtr() but for some reason it's not working as I expected when I use arrays as arguments.

Any ideas?
Thanks in advance. [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]
Link to comment
Share on other sites

[!--quoteo(post=377494:date=May 27 2006, 02:31 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ May 27 2006, 02:31 PM) [snapback]377494[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Here is my problem folks:

Sometimes when you need to do simple search and replace you can use str_replace. But let's say you created an encryption scheme, which consists in simple character swapping (like a->8, b->X, and so on). Obviously if you use str_replace it's not going to work, since it will seek the entire string everytime it's called, so some characters could be swapped twice, giving you a bad result.

So, I need something that won't "scan" something that was aleady replaced. I could use a complex code with preg_match , capturing the offsets, and using substr(), but this is kind of annoying. I also tried strtr() but for some reason it's not working as I expected when I use arrays as arguments.

Any ideas?
Thanks in advance. [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]
[/quote]

you can use preg_replace, in a non-greedy mod.
Link to comment
Share on other sites

Actually non-greedy regex won't help, since after each character it's going to search the entire string again. I don't want parts of the string that were already replaced to be replaced again.
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.