dth34653 2012-08-23 07:58
浏览 393
已采纳

如何将分号分隔的字符串放入数组?

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;
}
  • 写回答

4条回答 默认 最新

  • donglv9813 2012-08-23 08:06
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 openwrt双栈NAT
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用