I have strings that indicate an array by of a mix of "ordinary" notation as well as some ranges (some of which may be reversed), always bounded by an "equals" sign at the beginning and a space at the end (might not see that space at the end here), thus:

=94,102-108,109,205-200,400,420,436 

This represents the array:

94,102,103,104,105,106,107,108,109,200,201,202,203,204,205,400,420,436

Looking for a PHP function that would quickly extract the array.

    After cutting off the '=' and the ' ', use [man]explode/man on the comma to separate the string into single pages and page ranges. Go through the resulting list and use explode() again on the hyphen (which might or might not be there). If the hyphen was there, then the resulting array will have two elements, which can be turned into an array that includes the values in between as well by using [man]range[/man]. Then [man]array_merge[/man] all the resulting arrays together.

      Weedpacket;11048599 wrote:

      After cutting off the '=' and the ' ', use [man]explode/man on the comma to separate the string into single pages and page ranges. Go through the resulting list and use explode() again on the hyphen (which might or might not be there). If the hyphen was there, then the resulting array will have two elements, which can be turned into an array that includes the values in between as well by using [man]range[/man]. Then [man]array_merge[/man] all the resulting arrays together.

      Thank you for the pointers.

      Here's my embarrassingly clunky version:

      function get_string($string, $start, $end){
       $string = " ".$string;
       $pos = strpos($string,$start);
       if ($pos == 0) return "";
       $pos += strlen($start);
       $len = strpos($string,$end,$pos) - $pos;
       return substr($string,$pos,$len);
      }
      
      $TestString = '[sdjfdlsijgpigjdoijvds ids=94,102-108,109,205-200,400,420,436 kdkdjsohdoiahflouhlouh475bt9jd0skshe54y4nridhfud74h4jkr]';
      
      
      $isolated = get_string($TestString,'ids=',' ');
      
      $SinglesAndRanges = explode(',',$isolated);
      
      $FinalArray = array();
      
      foreach ($SinglesAndRanges as $instance) {
      
      if (strpos($instance,'-')) {
      $RangeEndValuesArray = explode('-',$instance);
      
      $RangeArray = range($RangeEndValuesArray[0],$RangeEndValuesArray[1]);
      foreach ($RangeArray as $this_instance) {
      array_push($FinalArray,$this_instance);
      }
      }
      else {
      array_push($FinalArray,$instance);
      }
      }
      
      
      foreach ($FinalArray as $instance) {
      echo '<br />'.$instance;
      }
      

        Here's what I came up with (just for fun?):

        <?php
        
        function parseIt($str)
        {
            return explode(',', preg_replace_callback(
                '/\b(\d+)-(\d+)\b/',
                function($matches) {
                    return(implode(',', range(min($matches[1], $matches[2]), max($matches[1], $matches[2]))));
                },
                trim($str, "= \t\n\r\0\x0B")
            ));
        }
        
        // Test:
        $test = '=94,102-108,109,205-200,400,420,436 ';
        $result = parseIt($test);
        print_r($result);
        

          Okay, if you're playing that.

          <?php
          
          function parseIt($sequence)
          {
          	$pages = call_user_func_array('array_merge', array_map(function($part)
          	{
          		$se = explode('-', $part);
          		return count($se) == 2 ? range($se[0], $se[1]) : $se;
          	}, explode(',', substr($sequence, 1, -1))));
          	sort($pages);
          	return $pages;
          }
          $test = '=94,102-108,109,205-200,400,420,436 ';
          $result = parseIt($test);
          print_r($result);
          
          

            LOL both of these are great - thanks guys 🙂

              Write a Reply...