Jhay
Jhay

Reputation: 77

How to put semicolon separated string into an array?

Well I was just wondering how can I put this semicolon separated string into an array

one,two,three; four,five,six; if( i < x { "seven,eight"; } if( x < i ) { nine; }

The result I need would be

[0] => one,two,three
[1] => four,five,six
[2] => if( i < x { "seven,eight"; } if( x < i ) { nine; }

I know how to explode those separated values but I'm stuck in this

if( i < x { "seven,eight"; } if( x < i ) { nine; }

The original data is this

bonus2 bAddRace,RC_DemiHuman,80; bonus2 bIgnoreDefRate,RC_DemiHuman,30; if(getrefine()>=6) { bonus2 bAddRace,RC_DemiHuman,40; } if(getrefine()>=9) {  autobonus2 "{ bonus bShortWeaponDamageReturn,20; bonus bMagicDamageReturn,20; }",200,1000,BF_WEAPON,"{ specialeffect2 EF_REFLECTSHIELD; }"; }

The code I've tried so far is this

function build_item_script_options( $data = array() )
{
    $html = "";
    $bonus_txt = "";
    $bonus_opts = "";
    $bonus_opts_array = array();

    $script = explode( ";", $data['script'] );
    for( $i = 0; $i <= count( $script ) - 1; $i++ )
    {
        if( strtolower( substr( $script[$i], 0, 5 ) ) == 'bonus' )
        {
            $bonus = explode( ',', $script[$i] );
            for( $k = 1; $k <= $bonus[$i] - 1; $k++ )
            {
                array_push( $bonus_opts_array, $bonus[$i][$k] );
            }
            $bonus_txt = $bonus[0];
            $bonus_opts = implode( ',', $bonus_opts_array );
        }
        else
        {
            $bonus_txt = $script[$i];
        }

        $html .= "<option opts=\"" . $bonus_opts . "\" value=\"" . $bonus_txt . "\" selected=selected>" . $bonus_txt . "</option>";
    }

    return $html;
}

Upvotes: 2

Views: 5753

Answers (4)

Josh
Josh

Reputation: 2895

Is this what you need?

$str = 'one,two,three; four,five,six; if( i < x { "seven,eight"; } if( x < i ) { nine; }';
$str = explode("; ", $str, 3);
print_r($str);

Example:

http://codepad.org/fqKMTUcp

** EDIT **

You could also try this:

$open_brackets = '0';
$string = '';
$str = 'one,two,three; four,five,six; if( i < x { "seven,eight"; } if( x < i ) { nine; }';
$split = str_split($str);
foreach($split as $bracket){
$string .= $bracket;
if($open_brackets == '0' && $bracket == ';'){
$string = substr_replace($string, "*", -1);
}
if($bracket == '{'){
$open_brackets++;
}
if($bracket == '}'){
$open_brackets--;
}
}
$string = explode('* ', $string);
print_r($string);

Example:

http://codepad.org/uO9sGnDz

Upvotes: 1

F.P
F.P

Reputation: 17831

You can't do that by simply exploding with ; - What you want to do there seems a lot like parsing a languae, for which you'd need an actual language parser that has intelligence, so it can decide wether or not a ; belongs to the first level or is inside other functions that don't need exploding.

You could e.g. count the amount of brackets you encounter as you move along the string and only explode if the ; occurss at at time when no brackets are opened.

Some psuedo code:

function explode_context_sensitive(string str) {
    int open_brackets = 0, last_semicolon = 0;
    string[] result = new string[];
    for (int idx = 0; idx < strlen(str); idx++) {
        if (str[idx] == ";" && open_brackets == 0) {
            result[] = substr(str, last_semicolon, idx);
            last_semicolon = idx;
        }
        else if (str[idx] == "(")
            ++open_brackets;
        else if (str[idx] == ")")
            --open_brackets;
    }
    return result;
}

You'll want to expand such a method to suit your needs, maybe counting different types of brackets or dealing with unsuited brackets...

In short: You're in for a lot of work if you want to do that right. Context-sensitive language parsing is extremely difficult if you don't want to recreate the whole grammar of the language.

Upvotes: 1

Gaurav
Gaurav

Reputation: 628

use * or some other special character not semicolon, because it may conflict sometimes as php recognize is as a statement breaker. try this :

$value = explode('*',$arr);

now you can access your array (arr) values by $value[0],$value[1] etc. also you have to use at the time of implode this:

$arr = implode('*',my,name,is,this);

this will produce as my*name*is*this. you can simply break it by using explode which is on top. happy coding!

Upvotes: 0

futuregeek
futuregeek

Reputation: 284

[2] => ( i < x ) ? "seven,eight":  "nine";

Is this what you are looking for?

Upvotes: -1

Related Questions