0

我有这个字符串(245)“- 当前地图 = VILLA - 当前任务 = VEGAS JUNKYARD、VILLA、PRESIDIO、KILL HOUSE、街道、三个王国赌场; - 可用任务 = TRAINYARD、IMPORT/EXPORT、MURDERTOWN、CQB 培训、炼油厂, 会议中心, 剧院;"

当前地图始终是一个值,当前任务和可用任务具有可变数量的值。


我想解析字符串,以便我有一个如下所示的数组:

Array ([Current Map] => VILLA [Current Missions] => Array ([0] => VEGAS JUNKYARD [1] => VILLA [2] => PRESIDIO [3] => KILL HOUSE [4] => STREETS [5] => THREE KINGDOMS CASINO)  [Availible Missions] => Array ([0] => TRAINYARD [1] => IMPORT/EXPORT [2] => MURDERTOWN [3]  => CQB TRAINING [4] => OIL REFINERY [5] => CONVENTION CENTER [6] => THEATER))

工作原理:它将 3 个主要部分(当前地图、当前任务、可用任务)分开

什么不起作用:将“当前任务”和“可用任务”的多个值分隔到一个数组中。

错误(或者我只是愚蠢):注意“可用任务”之前的分号

$search_strs_maps = array('Current Map = ', '- Current Missions = ', '; - Available  Missions = ', ';');

如果我把分号放在那里,即使我很确定分号在字符串中,它也找不到任何东西。


我提出这个的原因是,当我将分号放在“可用任务”之前时,我无法弄清楚为什么它没有找到任何东西,这让我发疯。此外,我还可以对代码进行第二次观察,并就如何使它变得更好提供一些建议。谢谢你的帮助!

这是我到目前为止所写的。

<?php
$str_map = '- Current Map = VILLA - Current Missions = VEGAS JUNKYARD, VILLA, PRESIDIO,  KILL HOUSE, STREETS, THREE KINGDOMS CASINO; - Available Missions = TRAINYARD, IMPORT/EXPORT, MURDERTOWN, CQB TRAINING, OIL REFINERY, CONVENTION CENTER, THEATER;';
$search_strs_maps = array('Current Map = ', '- Current Missions = ', '- Available  Missions = ', ';');
for ($i=0; $i<3; $i++){

                $str_map = trim ($str_map);
                output($str_map, 'Complete String');
                //Cut of the beginning : "- Current Map = "
                $start_pos = str_pos_last ($str_map,  $search_strs_maps[$i]);
                output($start_pos, 'Starting position');
                $str_map = substr($str_map, $start_pos);
                output($str_map, 'String after first cut:');
                //Cut the remaining part to "Current Missions ="
                $end_pos = strpos ($str_map, $search_strs_maps[$i+1]);
                output($end_pos, 'End position');
                //Save the new string without for example "VILLA"
                $ergebnis = substr($str_map, 0, $end_pos);
                $ergebnis = trim ($ergebnis);
                output($ergebnis, 'Finished result');
                $str_map = substr($str_map, $end_pos);
                echo '<hr>';
                if ($i==0){
                    $arr_return_strs['current_map'] = $ergebnis;
                }else{  //arrays
                    //stupid semicolon that doesnt get cut of
                    $ergebnis = str_replace ($str_map, ';', '');
                    $arr_maps_temp = array();
                    //count the number of commas, if there is one comma that means there are 2 words, there is no comma at the end
                    $map_count = substr_count ($ergebnis, ',')+1;
                    for ($ii=0;$ii<$map_count;$ii++){
                        echo '<hr';
                        //cut at first comma
                        $result_pos = strpos($str_map, ',');
                        //save
                        $result_map = substr($str_map, 0, $result_pos);
                        //cut of the result+comma
                        $ergebnis = substr ($ergebnis, $result_pos);
                        output($result_map, 'Result is:');
                    }
                }

            }
    echo '<hr><hr><hr>';
    print_r ($arr_return_strs);

function str_pos_last ($input_str, $search_str, $offset=0){
    //this function returns the endposition of a string we search for, strpos returns the beginning...
    $str_begin = strpos ($input_str, $search_str);
    $str_end = $str_begin + strlen($search_str);
    return $str_end;
}

function output ($var, $desc){
echo $desc.'<br>';
var_dump ($var);
echo '<hr>';
}
?>
4

4 回答 4

2
$initialString = "- Current Map = VILLA - Current Missions = VEGAS JUNKYARD, VILLA, PRESIDIO, KILL HOUSE, STREETS, THREE KINGDOMS CASINO; - Available Missions = TRAINYARD, IMPORT/EXPORT, MURDERTOWN, CQB TRAINING, OIL REFINERY, CONVENTION CENTER, THEATER;";


$resultArray = array();
$blockArray = explode('-',$initialString);
foreach($blockArray as $setArray) {
    if (!empty($setArray)) {
        list($dataArrayKey, $dataArrayValues) = explode('=',$setArray);
        $dataArrayKey = trim($dataArrayKey);
        $dataArrayValues = trim($dataArrayValues,' ;');
        if ($dataArrayKey == 'Current Map') {
            $resultArray[$dataArrayKey] = trim($dataArrayValues);
        } else {
            $dataArrayValues = explode('=', $dataArrayValues);
            foreach($dataArrayValues as $dataArrayValue) {
                $dataArrayEntries = explode(',',$dataArrayValue);
                $resultArray[$dataArrayKey] = $dataArrayEntries;
            }
        }
    }
}

var_dump($resultArray);

array
  'Current Map' => string 'VILLA' (length=5)
  'Current Missions' => 
    array
      0 => string 'VEGAS JUNKYARD' (length=14)
      1 => string ' VILLA' (length=6)
      2 => string ' PRESIDIO' (length=9)
      3 => string ' KILL HOUSE' (length=11)
      4 => string ' STREETS' (length=8)
      5 => string ' THREE KINGDOMS CASINO' (length=22)
  'Available Missions' => 
    array
      0 => string 'TRAINYARD' (length=9)
      1 => string ' IMPORT/EXPORT' (length=14)
      2 => string ' MURDERTOWN' (length=11)
      3 => string ' CQB TRAINING' (length=13)
      4 => string ' OIL REFINERY' (length=13)
      5 => string ' CONVENTION CENTER' (length=18)
      6 => string ' THEATER' (length=8)
于 2012-10-14T11:49:50.023 回答
1

为什么不使用http://php.net/preg_match

$str = "- Current Map = VILLA - Current Missions = VEGAS JUNKYARD, VILLA, PRESIDIO, KILL HOUSE, STREETS, THREE KINGDOMS CASINO; - Available Missions = TRAINYARD, IMPORT/EXPORT, MURDERTOWN, CQB TRAINING, OIL REFINERY, CONVENTION CENTER, THEATER;"

preg_match('~Current Map = (.*?) - Current Missions = (.*?); - Available Missions = (.*?);~ism',$str,$matches);
echo '<pre>';
print_r($matches);

Array
(
 [0] => Current Map = VILLA - Current Missions = VEGAS JUNKYARD, VILLA, PRESIDIO, KILL HOUSE, STREETS, THREE KINGDOMS CASINO; - Available Missions = TRAINYARD, IMPORT/EXPORT, MURDERTOWN, CQB TRAINING, OIL REFINERY, CONVENTION CENTER, THEATER;
 [1] => VILLA
 [2] => VEGAS JUNKYARD, VILLA, PRESIDIO, KILL HOUSE, STREETS, THREE KINGDOMS CASINO
 [3] => TRAINYARD, IMPORT/EXPORT, MURDERTOWN, CQB TRAINING, OIL REFINERY, CONVENTION CENTER, THEATER
)

然后你可以爆炸结果

于 2012-10-14T11:56:31.983 回答
1

与其他示例类似,但更简单($str输入字符串在哪里):

$types = array_map('trim', explode('-', substr($str, 1, strlen($str))));
$output = array();

foreach ($types as $value) {
  $type = array_map('trim', explode('=', str_replace(';', '', $value)));
  $opts = array_map('trim', explode(',', $type[1]));
  $output[$type[0]] = (count($opts) == 1) ? $opts[0] : $opts;      
}

var_dump($output);

输出

array
  'Current Map' => string 'VILLA' (length=5)
  'Current Missions' => 
    array
      0 => string 'VEGAS JUNKYARD' (length=14)
      1 => string 'VILLA' (length=5)
      2 => string 'PRESIDIO' (length=8)
      3 => string 'KILL HOUSE' (length=10)
      4 => string 'STREETS' (length=7)
      5 => string 'THREE KINGDOMS CASINO' (length=21)
  'Available Missions' => 
    array
      0 => string 'TRAINYARD' (length=9)
      1 => string 'IMPORT/EXPORT' (length=13)
      2 => string 'MURDERTOWN' (length=10)
      3 => string 'CQB TRAINING' (length=12)
      4 => string 'OIL REFINERY' (length=12)
      5 => string 'CONVENTION CENTER' (length=17)
      6 => string 'THEATER' (length=7)
于 2012-10-14T12:01:00.743 回答
0

这是你想要的:

<?php

$str = '- Current Map = VILLA - Current Missions = VEGAS JUNKYARD, VILLA, PRESIDIO, KILL HOUSE, STREETS, THREE KINGDOMS CASINO; - Available Missions = TRAINYARD, IMPORT/EXPORT, MURDERTOWN, CQB TRAINING, OIL REFINERY, CONVENTION CENTER, THEATER;';

$parts = str_replace(array('- ', ';'), '', explode(' - ', $str));

foreach($parts AS $key => $part){
    $parts[$key] = explode(' = ', $part);
    if($key != 0){
        $parts[$key][1] = explode(', ', $parts[$key][1]);
    }
}

$final_array = array();
foreach($parts AS $part){
    $final_array[$part[0]] = $part[1];
}

print_r($final_array);

?>

输出:

Array
(
    [Current Map] => VILLA
    [Current Missions] => Array
        (
            [0] => VEGAS JUNKYARD
            [1] => VILLA
            [2] => PRESIDIO
            [3] => KILL HOUSE
            [4] => STREETS
            [5] => THREE KINGDOMS CASINO
        )

    [Available Missions] => Array
        (
            [0] => TRAINYARD
            [1] => IMPORT/EXPORT
            [2] => MURDERTOWN
            [3] => CQB TRAINING
            [4] => OIL REFINERY
            [5] => CONVENTION CENTER
            [6] => THEATER
        )

)
于 2012-10-14T11:49:19.273 回答