You are here

function hashtags_get_tags in Hashtags 6

Same name and namespace in other branches
  1. 6.2 hashtags.module \hashtags_get_tags()
  2. 7.2 hashtags.module \hashtags_get_tags()
2 calls to hashtags_get_tags()
hashtags_filter in ./hashtags.module
Implementation of hook_filter().
hashtags_nodeapi in ./hashtags.module
Implementation of hook_nodeapi().

File

./hashtags.module, line 99

Code

function hashtags_get_tags($text, $capture_position = FALSE) {
  if ($capture_position) {

    // save position to avoid replacing hashtags inside links (<a hre="#ball">)
    $flag = PREG_OFFSET_CAPTURE;
  }
  else {
    $flag = 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
  // Bug when hashtag resides at the begining of the string - wrap text in <htest> tags for quick solution
  $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])) {
    return '';
  }

  // 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) . '"';
    }
    $result = implode(',', $tags_list[1]);
  }
  return $result;
}