I have this string(245) "- 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;"
Current Map is always one value, Current Missions and Availible Missions have a variable amount of values.
I would like to parse the string so that I have an array that looks like this:
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))
What works: It seperates the 3 big parts (Current Map, Current Missions, Availible Missions)
What doesn't work: Seperating the multiple values of "Current Missions" and "Availible Missions" into an array.
Bug (or I'm just stupid):Notice the semicolon before "Availible Missions"
$search_strs_maps = array('Current Map = ', '- Current Missions = ', '; - Available Missions = ', ';');
If I put the semicolon there it doesnt find anything even though I'm pretty sure the semicolon is in the string.
Reason I'm putting this up is that I can' figure out why it doesnt find anything when I put the semicolon before "Availible Missions" and it's driving me crazy. Also I could use a second set of eyes on the code and some advice on how to make it nicer. Thanks for your help!
This is what I have written so far.
<?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>';
}
?>