The first example shows the sections 26 & 35, and their associated township. If the sections are in different townships, it displays them as shown in Example 2. Sometimes only 1 section may be given.
What I want to do is return an array with all the information broken down.
The string “26&35-151-98” would be:
$Array[Section1] = 26;
$Array[Section1Township] = 151;
$Array[Section1Range] = 98;
$Array[Section2] = 35;
and so forth
So the & concatenates the two? Would it be safe to say that the pattern Section-Township-Range always exist at least once (or more) but that it is always just the three?
Yes, the pattern will always be like that. If theres only one section, then it will display exactly like that. “640 acres, section 20-34-152”, 34 being the township and 152 the range.
You may have to tweak this, but it is likely close to what you need.
$str = "35-151-98";
$str = preg_replace('/[\\&\\-]/', ',' , $str); // using regex to replace any '-' or '&' with a comma
$str_array = explode(',',$str); // explode the string into an array using the comma as a delimiter
$str_formatted = formatSectionsTownshipsRanges($str_array);
var_dump($str_formatted);
/*
*This function formats the Sections Townships and Ranges based on the repeating pattern STR
*/
function formatSectionsTownshipsRanges($str_array){
$count = count($str_array); // Count the total array
$pattern_index = 1; // used to track the pattern in threes
$section_num = 1; // section number that needs to be added onto the Section text in each array key
$str_final_array = array();
foreach($str_array as $value){
switch($pattern_index){
case 1:
$str_final_array["Section" . $section_num] = $value;
$pattern_index++;
break;
case 2;
$str_final_array["Section" . $section_num . 'Township'] = $value;
$pattern_index++;
break;
case 3;
$str_final_array["Section" . $section_num . 'Range'] = $value;
$section_num++;
$pattern_index = 1;
break;
}
$i++;
}
return $str_final_array;
}
So in the sting part 31-154-91&36-154-92 does this mean section1 = 31, section2=154, section3 =91, and section4 = 36, and they are in Township 154 and Range 92?
Basically if there is not a consistent pattern , for example if at times it can be 21&34&54-154-92 (where 21,34, and 54 are sections) and other times 31-154-91&36-154-92 mean two separate ranges where 31 and 36 are different sections in different townships and ranges, then it will be almost impossible to get a code algorithm to match. If, however we can say that anything infront of a & is a section and anything after is a township and range then it is very doable.
There are cases where it would be more sections; however, 99% of the time it will be formatted as Section&Section-Township-Range (both Sections having the same Township & Range), or Section-Township-Range&Section-Township-Range.
I wouldn’t care if the function failed to process sections beyond those two formats.
Yeah it seems hard, but maybe I can use preg_match to figure out if all the sections are within the same township/range, and then separate the values afterwards.