Data handling

I’m using file get contents to use information from other websites. The title attribute is what I want to use in the code shown below.

Example:

Spacing: <b><span title="1280 Acres, 2 Sections
Secs 26&35-151-98">2SEC</span></b>

Example 2:

Spacing: <b><span title="1280 Acres, 2 Sections
Sec: 31-154-91&36-154-92">2SEC</span></b>

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

Any ideas?

Hi,

You can get your sample string into an non-associative array using the following:


$text = "26&35-151-98";
$text = preg_replace('/[\\&\\-]/', ',' , $text);
$text_array = explode(',',$text);

Do you need more help after this?

Steve

Hey Steve, thanks for the reply! Do you have any recommendations for determining if the string contains different townships?

26&35-151-98
vs
31-154-91&36-154-92

Hi,

In those strings can you tell me what differentiates a township? Or is there a unique characteristic for townships i.e. always a number above 100?

The positioning in both goes Section-Township-Range. So in “26&35-151-98”, it means Section 26 and Section 27, in Township 151, Range 98"

In “31-154-91&36-154-92”, the sections are in different townships/ranges, so it displays it as Section-Township-Range & Section-Township-Range.

Does that make sense?

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;
}

This outputs:

array(3) { [“Section1”]=> string(2) “35” [“Section1Township”]=> string(3) “151” [“Section1Range”]=> string(2) “98” }

Regards,
Steve

The string will contain a Section-Township-Range at least once; however, what if it’s in the format as shown below?

26&35-151-98

This string contains Sections 26 and 35, which are both in Township 151, Range 98.

Is there the possibility for a variable number of sections more than 2; or is it either one or two sections?

For example you provided

Spacing: <b><span title="1280 Acres, 2 Sections
Sec: 31-154-91&36-154-92">2SEC</span>

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.

Steve

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.

Hi,

Sorry work got in my way of getting back to you with a potential fix.

I just whipped this up:


<?php
$str = "26&35-151-98";
$str = "31-154-91&36-154-92";

//Get the Values to the left of the &
$left = preg_replace('/\\&(\\d[-]?)*/', '', $str);
$left = explode('-', $left);
//Get the Values to the right of the &
$right = preg_replace('/^(\\d[-]?)*\\&/', '', $str);
$right = explode('-', $right);
// format the left and right values
$left_final = formatSectionsTownshipsRanges($left);
$right_final = formatSectionsTownshipsRanges($right, $section_num = 2);
// merge the two arrays together
$section_township_range = array_merge($left_final, $right_final);
// output the formatted values
$html = '<ol>';
foreach($section_township_range as $key => $value){
    $html .= '<li>'.$key.': '.$value.'</li>';
}
$html .= '</ol>';
echo $html;

function formatSectionsTownshipsRanges($str_array,$section_num = 1){
    $count = count($str_array);
    $pattern_index = 1;
    $str_final_array = array();
    $i = 0;
    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;
}

When the string = “26&35-151-98” then the output is

  1. Section1: 26
  2. Section2: 35
  3. Section2Township: 151
  4. Section2Range: 98

When the string = “31-154-91&36-154-92” then the output is

  1. Section1: 31
  2. Section1Township: 154
  3. Section1Range: 91
  4. Section2: 36
  5. Section2Township: 154
  6. Section2Range: 92

I hope this is more on track for you!

Regards,
Steve