0

私はこの文字列を持っています(245)"-現在のマップ=VILLA-現在のミッション=VEGAS JUNKYARD、VILLA、PRESIDIO、KILL HOUSE、STREETS、THREE KINGDOMSCASINO;-利用可能なミッション=TRAINYARD、IMPORT / EXPORT、MURDERTOWN、CQB TRAINING、OIL REFINERY 、コンベンションセンター、劇場;」

現在のマップは常に1つの値であり、現在のミッションと利用可能なミッションには可変量の値があります。


次のような配列になるように文字列を解析したいと思います。

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つの大きな部分(現在のマップ、現在のミッション、利用可能なミッション)を分離します

動作しないもの:「現在のミッション」と「利用可能なミッション」の複数の値を配列に分離します。

バグ(または私は愚かです):「AvailibleMissions」の前のセミコロンに注意してください

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

セミコロンをそこに置くと、セミコロンが文字列に含まれていると確信していても、何も見つかりません。


私がこれを掲げている理由は、「Availible Missions」の前にセミコロンを置いても何も見つからない理由がわからず、それが私を夢中にさせているからです。また、コードに2番目の目と、それをより良くする方法についてのアドバイスを使用することもできます。ご協力いただきありがとうございます!

これは私がこれまでに書いたものです。

<?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 に答える