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

  • SEARCH
  • Home
  • 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 6917001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:40:29+00:00 2026-05-27T09:40:29+00:00

Here’s something interesting I’ve just found out regarding str_replace(). In my function I need

  • 0

Here’s something interesting I’ve just found out regarding str_replace().
In my function I need to make two mysql queries which are the same with the slight change on sorting the result. As an argument I get string containing an order clause, for example ‘surname ASC’ but it could be ‘surname DESC’ as well. Now I wanted to easily switch to the reverse sorting using str_replace and I thought this should do the trick:

str_replace(array('ASC', 'DESC'), array('DESC', 'ASC'), $subject)

In my mind it should change all occurences of ASC with DESC and all occurences of DESC with ASC. Since the string contains only one of the two I should get reversed order clause.
However this is not the case. The output of the above code is the same string.

I did some testing and it tourned out that these calls do what you want them to do:

str_replace('ASC', 'DESC', $subject)
str_replace(array('ASC'), array('DESC'), $subject)

In my opinion this is weird because

array('ASC', 'DESC') != array('DESC', 'ASC')

Why then PHP would consider this as equal? Is there any other way to make this kind of replacement easily?

  • 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-05-27T09:40:30+00:00Added an answer on May 27, 2026 at 9:40 am

    In this scenario, you can use strtr like so:

    strtr($subject, array('ASC' => 'DESC', 'DESC' => 'ASC'));
    

    This will work because

    If given two arguments, the second should be an array in the form
    array('from' => 'to', ...). The return value is a string where all the
    occurrences of the array keys have been replaced by the corresponding
    values. The longest keys will be tried first. Once a substring has
    been replaced, its new value will not be searched again.

    See it in action.

    With str_replace what happens, as you have already found out, is that first ASC is replaced with DESC and then DESC is replaced back to ASC, for a grand total of nothing done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code, which takes two version identifiers in the form 1, 5,
Here's an interesting problem. On a recently installed Server 2008 64bit I opened IE
Here is my HTML. Just a simple form: <form> Username: <input type=text id=username/> <br/>
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here's my test function (c#, visual studio 2010): [TestMethod()] public void TestGetRelevantWeeks() { List<sbyte>
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here my code: $(document).ready(function() { $('#mid_select').live('click', function(e){ $('#middle').load( $(this).attr('href') + ' #middle'); var page
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs

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.