You are here

function hashtags_parse_tags in Hashtags 7

3 calls to hashtags_parse_tags()
hashtags_comment_presave in ./hashtags.module
Implements hook_comment_presave().
hashtags_entity_presave in ./hashtags.module
Implementation of hook_entity_presave().
_hashtags_filter_process in ./hashtags.module

File

./hashtags.module, line 713

Code

function hashtags_parse_tags($text, $is_string_return = TRUE, $capture_position = FALSE) {

  // capture_position == PREG_OFFSET_CAPTURE;
  // save position to avoid replacing hashtags
  // inside links (<a hre="#ball">)
  $flag = $capture_position ? PREG_OFFSET_CAPTURE : PREG_PATTERN_ORDER;
  $tags_list = array();

  // 1) 2+ character after #
  // 2) Don't start with or use only numbers (0-9) (#123abc, #123 etc)
  // 3) Letters - digits work correct (#abc123, #conference2013)
  // 4) No Special Characters “!, $, %, ^, &, *, +, .”
  // 5) No Spaces
  // 6) May use an underscore. Hyphens and dashes will not work.
  // 7) <p>#hashtag</p> - is valid
  // 8) <a href="#hashtag">Link</p> - is not valid
  $pattern = "/[\\s>]+?#([[:alpha:]][[:alnum:]_]*[^<\\s[:punct:]])/iu";

  // add <htest> to process first #hastag - string beginning
  preg_match_all($pattern, '<htest>' . $text . '<htest>', $tags_list, $flag);

  // no hashtags has been found
  if (isset($tags_list[0]) && !sizeof($tags_list[0])) {
    if ($is_string_return) {
      return '';
    }
    return array();
  }

  // save position
  if ($capture_position) {
    foreach ($tags_list[1] as $key => $data) {

      // array[position] = hashtag
      $result[$data[1]] = strtolower($data[0]);
    }
  }
  else {

    // turn tags into lowercase
    foreach ($tags_list[1] as $key => $tag) {
      $tags_list[1][$key] = strtolower($tag);
    }
    if ($is_string_return) {
      $result = implode(',', $tags_list[1]);
    }
    else {
      $result = $tags_list[1];
    }
  }
  return array_unique($result);
}