Php convert hash into hashtags from content

1.2K views
no comments
25 Feb 2016 12:08 am

Mostly the largest social networks like #facebook , #twitter , #soundcloud and other application are using #hashtag functionality to sort their data and live-feed or content. So I have wrote this  tutorial to convert # from content and convert into a #hashtag

1. Define function named sdtutshashtag with a single parameter $post_content

In $post_content parameter we can pass data plain text or html, in case user post text from textarea box or html passed from wordpress post content

function sdtutshashtag($post_content){
}

now are working in under function

2. Clean html tags with strip_tags

#php function strip_tags return the plain text and remove all html tags because #hash also used in colors code and it will convert a css color property into hash tag

$plain_post_content = strip_tags($post_content);

3. Again clean printed html codes

SDTuts and other tutorials blogs also using syntax highlighter to highlight the code syntax so we put printed quotes languages and it will also convert hashtag used # in code we need to also strip these printed tags.
First convert printed code into html and then clean the html tags into plain text.

$plain_post_content = html_entity_decode($plain_post_content);
$plain_post_content = strip_tags($plain_post_content);

4. Match words started with hash

This is interested and part we use regular expression to extract the words who start with # and must be alphabetic or numeric, with the function of preg_match_all, first parameter is #regex second parameter of data/text and third parameter output.

Explanation of regular expression pattern used to extract hash with words

Slash (/) starting boundary of regex
Hash (#) first letter of word or a word start (#)
Bracket ([) Matching character set
Backslash and W (\w) and a word containing alphabetic numeric letters and this is short alternative of a-zA-Z0-9
Plus (+) plus sign use for one or more entities
Slash (/) ending boundary of regex
preg_match_all('/#[\w]+/',$plain_post_content,$all_tags);

5. Define url of your domain

Define your domain url for create anchor hyper reference href because we can redirect to shorting hash tag page I have defined my domain name with tag

$tags_url = 'https://sdtuts.com/tag/';

6. Remove duplicates of hashtags from extracted tags in array

remove duplicates array with array_unique because if we don’t remove duplicates arrays it will replace hash tags again and again, example if we have three same words of hash SDTuts.

<a href="#"><a href="#"><a href="#">#SDTuts</a></a></a>
$all_tags = array_unique($all_tags[0]);

7. Run foreach loop

Run foreach loop from $all_tags to replace one by one word

function sdtutshashtag($post_content){
}

Now we are code in foreach loop

8. Define two variables plain_tag and new_hashtag

One variable plain_tag is used for url, alt title attributes and second variable new_hashtag is used for new hashtag value with anchor tag

$new_hashtag = '<a href="'.$tags_url.$plain_tag.'" class="hash_tag" title="posts under '.$plain_tag.'">'.$tagvalue.'</a>';
$plain_tag  = str_replace('#','',$tagvalue);

9. Replace with preg_replace function with regex

At this str_replace is not enough good to replace the content because I have tested it replace word eg two matching hash tag found eg sdtuts_hashtag and sdtuts_hashtag_another and str_replace will replace the both matching sdtuts_hashtag and leave _another as text.

$post_content = preg_replace('/#\b'.$plain_tag.'\b/u', $new_hashtag, $post_content);

Now loop code is end and we are outside of foreach loop

10. Now return post_content

return $post_content;

Now function is ended and we are outside of function

Register filter for wordpress hashtag integration

Register filter hook called the_content and second parameter function that we defined in this tutorial but don’t worry I have added full code of this tutorial, and third parameter is used for priority

add_filter('the_content','sdtutshashtag',999999999999999999999);

Here is full code of this tutorial
Scroll down to set latest and bug fixes full code

function sdtutshashtag($post_content){
	$plain_post_content = strip_tags($post_content);
	$plain_post_content = strip_tags(html_entity_decode($plain_post_content));
	preg_match_all('/#[\w]+/',$plain_post_content,$all_tags);
	$tags_url = get_bloginfo('url').'/tag/';
	$all_tags = array_unique($all_tags[0]);
	foreach($all_tags as $tagindex=>$tagvalue){
		$plain_tag  = str_replace('#','',$tagvalue);
		$new_hashtag = '<a href="'.$tags_url.$plain_tag.'" class="hash_tag" title="posts under '.$plain_tag.'">'.$tagvalue.'</a>';
		$post_content = preg_replace('/#\b'.$plain_tag.'\b/u', $new_hashtag, $post_content);

	}
	return $post_content;
}
add_filter('the_content','sdtutshashtag',999999999999999999999);

New full code of tutorial many bugs are fixed

function sdtutshashtag($postcontent){
	$already_url = array();
    $plain_post_content = strip_tags(strip_shortcodes($postcontent));
    $plain_post_content = strip_tags(html_entity_decode($plain_post_content));
    preg_match_all('/#[\w]+/', $plain_post_content, $all_tags);
	$get_bloginfo_url = get_bloginfo('url');
	$all_tags = array_unique($all_tags[0]);
    if (is_objarray($all_tags)) {
        foreach ($all_tags as $tagindex => $tagvalue) {
            if (!in_array($tagvalue, $all_tags)) {
                continue;
            }
            $plain_tag = str_replace('#', '', $tagvalue);

            ///LINE FOR IGNORE CSS COLORS - START
            if (strlen(preg_replace('/[a-fA-F0-9]/', '', $plain_tag)) <= 0) {
                continue;
            }
            ///LINE FOR IGNORE CSS COLORS - END
			
			$plain_tag_lowercase = strtolower($plain_tag);
			if(isset($already_url[$plain_tag])){
				$hastag_href = $already_url[$plain_tag];
			} else {				
				$tagterm_data = get_term_by('name', $plain_tag_lowercase, 'post_tag');
				if(!isset($tagterm_data->slug)){
					$tagterm_data = get_term_by('name', $plain_tag_lowercase, 'category');
				}				
				if($tag_category_id = get_cat_ID($plain_tag_lowercase)){
					$hastag_href = get_category_link($tag_category_id);
				} else if(isset($tagterm_data->slug)){
					$hastag_href = $get_bloginfo_url.'/tag/'.$tagterm_data->slug;
				} else {
					$hastag_href = $get_bloginfo_url.'?s='.$plain_tag;
				}	
				
				$already_url[$plain_tag_lowercase] = $hastag_href;
			}
            $new_hashtag = '<a href="' . $hastag_href . '" class="hash_tag" title="posts under ' . $plain_tag . '">' . $tagvalue . '</a>';
            $postcontent = preg_replace('/#\b' . $plain_tag . '\b/u', $new_hashtag, $postcontent);
	
        }
        return $postcontent;
    }
    return $postcontent;
}

add_filter('the_content', 'sdtutshashtag', 999999999999999999999);

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>