Search code examples
phparraysxmlassociative-arraytext-extraction

Get pairs of substrings from a block text and form into an associative array


How can I filter my content and display those values together elsewhere. The content is being loaded in via XML that is being filled by our client. Therefore I can't change the values. The problem is that my values are on different lines. I am currently filtering some values:

if (!empty($arraywithvalues)) {
    $array = explode("<br>", $arraywithvalues);
    foreach( $array as $key => $value){
        if (
            strpos(strtolower($value),'oppervlakte') !== FALSE ||
            strpos(strtolower($value),'perceel') !== FALSE ||
            strpos(strtolower($value),'golf') !== FALSE 
        ) {
            $filtered_content[] = $value;
            unset($array[$key]);
        }
    };

This is to filter out the text bit, this is succesfully done as can be seen on this website. Problem is there are still values that need to be filitered out like:

20 M2

20 km

However the filtering itself is no problem. The problem im running into is putting the values next to one and another. So for example. Distance till golf (First filtered value) - 20km (Second filtered value). How can I achieve this?

XML as it gets imported:

string(82)"

Bebouwde oppervlakte

80 M2

Terras oppervlakte

20 M2

Keuken oppervlakte

12 M2"

Solution

  • PHP8

    <?php
    
    $values = <<<EOL
    
    Bebouwde oppervlakte
    
    80 M2
    
    Terras oppervlakte
    
    20 M2
    
    Keuken oppervlakte
    
    12 M2
    EOL;
    
    if (!$values) {
        // you can throw an exception and/or log etc.
        return;
    }
    
    $valueList = array_filter(explode(PHP_EOL, $values));
    $valueList = array_values(array_map('trim', $valueList));
    
    $formatedValueList = [];
    
    foreach($valueList as $key => $value) {
        if ((
                str_contains(strtolower($value), 'oppervlakte')
                || str_contains(strtolower($value), 'perceel') 
                || str_contains(strtolower($value), 'golf')
            ) && (
                str_contains(strtolower($valueList[$key+1]), 'm2')
                || str_contains(strtolower($valueList[$key+1]), 'km')
            )
        ) {
            $formatedValueList[] = sprintf('%s - %s', $value, $valueList[$key+1]);
        }
    }
    
    
    var_dump($formatedValueList);
    

    PHP7

    <?php
    
    $values = <<<EOL
    
    Bebouwde oppervlakte
    
    80 M2
    
    Terras oppervlakte
    
    20 M2
    
    Keuken oppervlakte
    
    12 M2
    EOL;
    
    if (!$values) {
        // you can throw an exception and/or log etc.
        return;
    }
    
    $valueList = array_filter(explode(PHP_EOL, $values));
    $valueList = array_values(array_map('trim', $valueList));
    
    $formatedValueList = [];
    
    foreach($valueList as $key => $value) {
        if ((
                strpos(strtolower($value), 'oppervlakte') !== false
                || strpos(strtolower($value), 'perceel')  !== false
                || strpos(strtolower($value), 'golf') !== false
            ) && (
                strpos(strtolower($valueList[$key+1]), 'm2') !== false
                || strpos(strtolower($valueList[$key+1]), 'km') !== false
            )
        ) {
            $formatedValueList[] = sprintf('%s - %s', $value, $valueList[$key+1]);
        }
    }
    
    
    var_dump($formatedValueList);
    

    Output

    array(3) {
      [0]=>
      string(28) "Bebouwde oppervlakte - 80 M2"
      [1]=>
      string(26) "Terras oppervlakte - 20 M2"
      [2]=>
      string(26) "Keuken oppervlakte - 12 M2"
    }