我有这个 string(245)“ - 当前地图= VILLA - 当前任务= VEGAS JUNKYARD,VILLA,PRESIDIO,KILL HOUSE,STREETS,三国赌场; - 可用任务= TRAINYARD,进口/出口,MURDERTOWN,CQB TRAINING,OIL REFINERY,CONVENTION CENTER ,剧院;“
当前地图总是一个值, 当前任务和可用任务具有可变数量的值。
我想解析字符串,以便我有一个如下所示的数组:
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个大部分(当前地图,当前任务,可用任务)
什么行不通:将“当前任务”和“可用任务”的多个值分隔成一个数组。
Bug(或者我只是愚蠢):注意“可用任务”之前的分号
$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>';
}
?>
答案 0 :(得分: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)
答案 1 :(得分: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
)
然后你可以爆炸结果
答案 2 :(得分: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)
答案 3 :(得分: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
)
)