PHP simplify range creation

556

I wrote this code to generate range of numbers.

Can someone help me simplify this code. I assume that it can be done in much more simpler and cleaner way, but don't know how ?

$variant_ids = 'AB0000001, AB0000002, AB0000003 - AB0000010, AB0000011 - AB0000020, AB0000021, AB0000022';
$delimiter = ',';
$range_delimiter = '-';

$variant_ids = array_filter(array_map('trim', explode($delimiter, $variant_ids)), 'strlen');

if (is_array($variant_ids)) {
    foreach ($variant_ids as &$variant_id) {
        if (strpos($variant_id, $range_delimiter) !== FALSE && substr_count($variant_id, $range_delimiter) == 1) {
            $variant_range_id = array_map('trim', explode($range_delimiter, $variant_id));
            $variant_id = array();
            for ($i = $variant_range_id[0]; $i <= $variant_range_id[1]; $i++) {
                $variant_id[] = $i;
            }
            $variant_id = implode($delimiter, $variant_id);
        }
    }
}

$variant_ids = implode($delimiter, $variant_ids);

echo '<pre>'; print_r($variant_ids); echo '</pre>';
296

Answer

Solution:

Interesting concept that I believe that PHP’s built in function can handle well. Reworked your logic to work with the concept as arrays using , & some regex juggling. Not perfect, but based on your original code’s needs it seems to be a nice alternative if I do say so myself.

// Set the basics
$raw_variant_ids = 'AB0000001, AB0000002, AB0000003 - AB0000010, AB0000011 - AB0000020, AB0000021, AB0000022';
$delimiter = ',';
$range_delimiter = '-';
$variant_ids = array();

// Explode the $raw_variant_ids to generate an array.
$range_array = explode($delimiter, $raw_variant_ids);

// Roll through the $range_array.
foreach ($range_array as $range_value) {

  // Split the range value.
  $range_split = explode($range_delimiter, $range_value);

  // If the range split has two items, generate a range. Or else, just as a straight id.
  if (count($range_split) == 2) {

    // Get the numbers for the start & end of the range.
    $range_number_start = trim(preg_replace('/[a-zA-Z]/is', '', $range_split[0]));
    $range_number_end = trim(preg_replace('/[a-zA-Z]/is', '', $range_split[1]));

    // Get the letters for the start of the range. 
    $alpha_prefix = trim(preg_replace('/[0-9]/is', '', $range_split[0]));

    // Get the length of the numbers based on the start number.
    $id_length = strlen($range_number_start);

    // Now use 'range()' to generate a range.
    $range_span_array = range($range_number_start, $range_number_end);

    // Roll through the $range_span_array.
    foreach ($range_span_array as $range_span_array_value) {
      $variant_ids[] = $alpha_prefix . str_pad($range_span_array_value, $id_length, 0, STR_PAD_LEFT);
    }
  }
  else {
    foreach ($range_split as $range_split_value) {
      $variant_ids[] = trim($range_split_value);
    }
  }

}

echo '<pre>';
print_r($variant_ids);
echo '</pre>';

People are also looking for solutions to the problem: javascript - AJAX call working in Chrome, but not in Safari, Firefox or Internet Explorer

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.