Jump to content


Photo

"Always Progressive" str_replace?


  • Please log in to reply
3 replies to this topic

#1 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 27 May 2006 - 04:31 AM

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\" /]
~ D Kuang

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 27 May 2006 - 05:12 AM

[!--quoteo(post=377494:date=May 27 2006, 02:31 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ May 27 2006, 02:31 PM) View Post[/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.
Life's too short for arguing.

#3 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 28 May 2006 - 03:41 AM

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.
~ D Kuang

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 May 2006 - 10:27 AM

I would have suggested strtr().

What is the particular piecs of code that isn't working?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users