Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8046907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:56:21+00:00 2026-06-05T05:56:21+00:00

php has the strtr function: strtr(‘aa-bb-cc’, array(‘aa’ => ‘bbz’, ‘bb’ => ‘x’, ‘cc’ =>

  • 0

php has the strtr function:

strtr('aa-bb-cc', array('aa' => 'bbz', 'bb' => 'x', 'cc' => 'y'));
# bbz-x-y

It replaces dictionary keys in a string with corresponding values and (important) doesn’t replace already replaced strings. A naive attempt to write the same in python:

def strtr(strng, replace):
    for s, r in replace.items():
        strng = strng.replace(s, r)
    return strng

strtr('aa-bb-cc', {'aa': 'bbz', 'bb': 'x', 'cc': 'y'})

returns xz-x-y which is not we want (bb got replaced again). How to change the above function so that it behaves like its php counterpart?

(I would prefer an answer without regular expressions, if possible).

Upd: some great answers here. I timed them and found that for short strings Gumbo’s version appears to be the fastest, on longer strings the winner is the re solution:

# 'aa-bb-cc'
0.0258 strtr_thg
0.0274 strtr_gumbo
0.0447 strtr_kojiro
0.0701 strtr_aix

# 'aa-bb-cc'*10
0.1474 strtr_aix
0.2261 strtr_thg
0.2366 strtr_gumbo
0.3226 strtr_kojiro

My own version (which is slightly optimized Gumbo’s):

def strtr(strng, replace):
    buf, i = [], 0
    while i < len(strng):
        for s, r in replace.items():
            if strng[i:len(s)+i] == s:
                buf.append(r)
                i += len(s)
                break
        else:
            buf.append(strng[i])
            i += 1
    return ''.join(buf)

Complete codes and timings: https://gist.github.com/2889181

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:56:24+00:00Added an answer on June 5, 2026 at 5:56 am

    Here is a naive algorithm:

    Use an index to walk the original string character by character and check for each index whether one of the search strings is equal to the string from the current index on. If a match is found, push the replacement in a buffer and proceed the index by the length of the matched string. If no match is found, proceed the index by one. At the end, concatenate the strings in the buffer to a single string.

    def strtr(strng, replace):
        buffer = []
        i, n = 0, len(strng)
        while i < n:
            match = False
            for s, r in replace.items():
                if strng[i:len(s)+i] == s:
                    buffer.append(r)
                    i = i + len(s)
                    match = True
                    break
            if not match:
                buffer.append(strng[i])
                i = i + 1
        return ''.join(buffer)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP has a function extract that will convert an array like this: $array =
Is there a function in Java which is equivalent to php's strtr ? string
PHP has a great function called htmlspecialcharacters() where you pass it a string and
PHP has this wonderful function, strtotime , that takes any string containing just about
Php has a method escapeshellcmd() that escapes any characters in a string that might
PHP has a is_readable function which checks to see if the file is readable
My routes.php has the following line of code: Router::connect('/', array('controller' => 'users', 'action' =>
PHP has a var_dump() function which outputs the internal contents of an object, showing
PHP has a function range('a','z') which prints the English alphabet a, b, c, d,
PHP has a built-in function that takes a data and returns the day of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.