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 4071696
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:47:24+00:00 2026-05-20T16:47:24+00:00

I have PHP code and i am translating it to Java, but how do

  • 0

I have PHP code and i am translating it to Java, but how do we do PHP like explode(), list(), foreach() with Java? Following PHP code are my example problems:

1. PHP:
  /** 
   * @return: Array
   */
    public function parse($digest) 
    {
      $array = array();
      $parts = explode(", ", $digest);

      foreach ($parts as $x) 
      {
       $bits = explode("=", $x);
       $data[$bits[0]] = str_replace('"','', $bits[1]);
      }

      return $array;
    }

Java (updated):
  private String[] parse(String digest)
  {
    Map<String,String> array = new HashMap<String, String>();
    String[] parts = digest.split(", ");

    for (String x:parts)
    {
      String[] bits = x.split("=");
      array.put(bits[0], bits[1].replace( '"','' )); // later will apply regex
    }

    return array; // incompatible types
  }



2. PHP:
  /** 
   * @return: String
   */
  public function response($text, $user, $pass, $httpmethod, $uri) 
  {        
      list($dummy_digest, $value) = split(' ', $text, 2);    
      $x = $this->parse($value);

      $realm = $x['realm'];
      if ($x['qop'] == 'auth') 
      {
        ...
      }

      $base  = "{$realm}, {$x['realm']}";
      //"Digest username="1", realm="1", nonce="xxxxxx", uri="abc", response="abc", qop="auth", algorithm="MD5", cnonce="", nc="1"";
      return $base;

  }


Java problem:
   public String response(String text, String user, String pass, String httpmethod, String uri)
   {  
      // how to do such list() like php?
      return "Digest username="1", realm="1", nonce="xxxxxx", uri="abc", response="abc", qop="auth", algorithm="MD5", cnonce="", nc="1"";
   } 
  • 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-20T16:47:24+00:00Added an answer on May 20, 2026 at 4:47 pm

    it’s pretty much the same going to java, you could parse the data with:

    Map<String, String> key_map = new HashMap<String, String>();
    String[] parts_of_string = "var=test, var2=test2".split(", ");
    for(String part : parts_of_string) {
        String[] key_and_value_parts = part.split("=");
        key_map.put(key_and_value_parts[0], key_and_value_parts[1]);
        System.out.println(key_and_value_parts[0] + " <-> " + key_and_value_parts[1]);
    }
    

    Update (Second Method):

    Java does not handle arrays quite the same as PHP. Where php can refrence a object in array by using a string like $array[“my_var”] , Java can only reference by their index in the array. So you will need to use a map, this allows you to map a certain key to a value. You could parse the text like so:

    private static Map<String, String> parse(String digest) {
        String[] parts = digest.split(", ");
        Map<String, String> map = new HashMap<String, String>();
        for (String x : parts) {
            String[] bits = x.split("=");
    
            //Remove "'s here if you want to keep them in the string
    
            map.put(bits[0], bits[1]);
        }
        return map;
    }
    

    You can then access any information given by this map by using the method get on it. You will then specify the key you want and the map will return the value associated with that key. Be sure to use .containsKey to check if the Ma contains the key you are looking for.

    So when you have parsed your information with the previous function you can then access the values by using this for example:

    Map<String, String> parsed_map = parse("A realm=\"1234\", B=\"3434df323423423\", C=\"300.00\", D=\"loopback\"'");
    String info = parsed_map.get("B");
    System.out.println(info);
    

    Hope that helps, else ask away 😀

    PS: I haven’t actually tested this just be sure to check that it runs as expected.

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

Sidebar

Related Questions

I have the following php code, which has been snipped for brevity. I am
I have a bit of PHP code which I need to return an even
I like Doxygen to create documentation of C or PHP code. I have an
I'm mostly a Rails developer but sometimes, I have to code in PHP. Because
I have this php code, with which I am trying to generate a popup
I have this PHP code echo '<a href=# onclick=updateByQuery(\'Layer3\', ' . json_encode($query) . ');>Link
Say i have this PHP code: $FooBar = a string; i then need a
I have inherited some legacy PHP code what was written back when it was
I have a basic ajax application, which will not work, instead the php code
I have a code snippet written in PHP that pulls a block of text

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.