You are here

function webform_structured_text_parse_mask in Webform Structured Text 6

Same name and namespace in other branches
  1. 7 structured_text.inc \webform_structured_text_parse_mask()

Helper function to parse a mask string into an array of mask parts.

Parameters

string $mask The mask input by the user when the component was configured.:

Return value

array Mask parts as a zero-indexed array, with the elements of the array itself an array of: 'type' => one of '9', 'a', 'x', 'r', or 'markup', 'length' => the length of that part of the mask, or max-length where 'type' == 'r', 'value' => the actual markup in the case of 'type' == 'markup', empty otherwise, 'size' => where 'type' == 'r', the optional size of the field.

6 calls to webform_structured_text_parse_mask()
webform_structured_text_field_description in ./structured_text.inc
Helper function to generate the description for the control, optionally indluding input format instructions / description.
webform_structured_text_format_value in ./structured_text.inc
Helper function to format a value per a mask.
webform_structured_text_i18n_update_strings in ./webform_structured_text.module
Update translatable strings.
webform_structured_text_parse_value in ./structured_text.inc
Helper function to take a string value and parse it into an array based on a mask.
_webform_render_structured_text in ./structured_text.inc
Implements _webform_render_component().

... See full list

File

./structured_text.inc, line 787

Code

function webform_structured_text_parse_mask($mask) {
  $mask_array = array();
  $mask = (string) $mask;
  $mask_length = drupal_strlen($mask);
  $where = '';
  $part = -1;
  for ($i = 0; $i < $mask_length; $i++) {
    $type = in_array($mask[$i], array(
      '9',
      'a',
      'x',
      'r',
    )) ? $mask[$i] : 'markup';
    if ($where != $type) {
      $where = $type;
      $mask_array[++$part] = array(
        'type' => $type,
        'length' => 0,
        'value' => '',
      );
    }
    if ($type == 'r') {

      // digits after an 'r' mask give max length and size if not followed by a comma and other digits
      if (preg_match('/[1-9]/', $mask[$i + 1])) {

        // there's at least one digit
        $i++;
        while ($i < $mask_length && preg_match('/[0-9]/', $mask[$i])) {
          $mask_array[$part]['length'] .= $mask[$i++];
        }
        $mask_array[$part]['length'] = (int) $mask_array[$part]['length'];
        if ($i < $mask_length && $mask[$i] == ',' && $i + 1 < $mask_length && preg_match('/[1-9]/', $mask[$i + 1])) {

          // a number after the commas specifies the display size of the field
          $i++;
          $mask_array[$part]['size'] = '';
          while ($i < $mask_length && preg_match('/[0-9]/', $mask[$i])) {
            $mask_array[$part]['size'] .= $mask[$i++];
          }
          $mask_array[$part]['size'] = (int) $mask_array[$part]['size'];
        }
        $i--;

        // back up the counter for regular expressions because it will be advanced by the for loop.
      }
      else {
        $mask_array[$part]['length'] = 1;
      }
    }
    else {
      $mask_array[$part]['length']++;
    }
    if ($type == 'markup') {
      if ($mask[$i] == '^') {
        if ($i + 1 < $mask_length && in_array($mask[$i + 1], array(
          '9',
          'a',
          'x',
          'r',
          '^',
        ))) {
          $mask_array[$part]['value'] .= $mask[++$i];
        }
      }
      else {
        if ($mask[$i] == ' ') {

          // Special treatment for spaces.  Start off a string of spaces with
          // a regular one, and then toggle between non-breaking and reagular
          // to achieve the spacing specified in the mask.
          if ($i == 0 || $mask[$i - 1] != ' ') {

            // Reset space character if it's the first in a series.
            $space = '&nbsp;';
          }
          $space = $space == ' ' ? '&nbsp;' : ' ';
          $mask_array[$part]['value'] .= $space;
        }
        else {
          $mask_array[$part]['value'] .= $mask[$i];
        }
      }
    }
  }
  return $mask_array;
}