function _hashtags_parse_tags in Hashtags 8
Parse a string and return an array or comma-separated string of tags depending on the mode (second parameter)
Parameters
$text:
bool $is_string_return:
bool $capture_position:
Return value
array|string
2 calls to _hashtags_parse_tags()
- hashtags_entity_presave in ./
hashtags.module - Implements hook_entity_presave().
- _hashtags_get_tids_by_text in ./
hashtags.module - Get taxonomy term ids by text that contains hashtags
File
- ./
hashtags.module, line 302
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 href="#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) {
$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);
}