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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:02:31+00:00 2026-06-08T19:02:31+00:00

function parseCommandString( $str ) { $chunks = explode( ”, $str ); $cmd = array();

  • 0
function parseCommandString( $str ) {
    $chunks = explode( '"', $str );
    $cmd = array();

    // Split the command into space-separated parameters (preserving quoted portions)
     $c = count($chunks);
    for( $i=0; $i<$c; $i++ ) {
        if( $i % 2 == 0 ) {
            if( $chunks[$i] == '' ) {
                continue;
            }

            $params = explode( ' ', $chunks[$i] );
             $pc = count($params);
            for( $j=0; $j<$pc; $j++ ) {
                if( $params[$j] != '' ) {
                    $cmd[] = $params[$j];
                }
            }
        } else {
            if( $chunks[$i] != '' ) {
                $cmd[] = $chunks[$i];
            }
        }
    }
    return $cmd;
}

Seems simple enough, but I lack insight on whether this is the best implementation for what I want to do. That is, I simply need to break apart a string into an array of substrings first by sections of quotes, then by spaces (within non-quoted sections, that is). It is meant to be like an actual command prompt on my site.

Insight appreciated.

  • 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-08T19:02:34+00:00Added an answer on June 8, 2026 at 7:02 pm

    Here is how I would approach this project:

    1) Start with tests. For example.

    isEqual(parseCommandString('hello world', ["hello", "world"]));
    isEqual(parseCommandString('hello "world a"', ["hello", "world a"]));
    

    2) Make your code fit those tests, and then add more corner cases – for example, unclosed quotes.

    If what you’re trying to build is a true command line (i.e. a shell), consider using an open source project, ex. http://code.google.com/p/php-web-shell/

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

Sidebar

Related Questions

function ProvideValue(){ Values = document.getElementById('HiddenValue').value; FirstCut = Values.split(@#@); // This will return the array
function killsession() { // global $_SESSION; $_SESSION = array(); if (session_id() != || isset($_COOKIE[session_name()]))
function test() { $content = lang=en]text en|lang=sp]text sp; $atts = explode('|', $content); } What
function test() { alert(Array.join.call(arguments,'/')) //alerts / alert(Array.prototype.join.call(arguments,'/')) //alerts Js/JScript alert(Array.join(arguments,'/')) //alerts Js/JScript } test('Js','JScript');
function heaviside(&$value, $key, &$array) { if($key > 0) $value = $array[$key-1].$array[$key]; } function test_heaviside()
function a($function, $array) { global $test $test->$function(implode(',' $array)); } For example, I want to
function returnsAnArray () { return array ('test'); } echo returnsAnArray ()[0]; generates a syntax
function get_last_word($sentance){ $wordArr = explode(' ', $sentance); $last_word = trim($wordArr[count($wordArr) - 1]); runDebug( __FILE__,
function classes(o:integer): String; var allclasses : array[1..7] of String; begin allclasses[1]:= 'class1'; allclasses[2]:= 'class2';
Function parameters tend to have different purposes : in most case they are just

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.