RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts</strong | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.39% (6.67%*) • Fixed: 5.39% (6.67%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.69% (6.52%*) • Investment PI: 5.49% (5.98%*)

Extract #Hashtag Keywords from a String of Text with PHP

Extract #Hashtag Keywords from a String of Text with PHP

A hashtag is a word or an unspaced phrase or word prefixed with the hash symbol (#) that is used on various social websites to create (often distinct) social search terms that group various types of users and data (often collating data from various platforms). By clicking on a hashtag (essentially just a search term), you'll be connected with other people that are engaging on the same topic.

At least a few sites we've involved ourselves with lately utilise hashtags in one way or another. This post will show you how to extract hashtags from a string of text (using a basic function) and return the result as either an array or string of text.

Return Hashtag Array

The first function returns an array with all hashtags.

1
<?php 
2
/*
3
 Extract #Hashtag Keywords from a String of Text with PHP
4
 http://www.beliefmedia.com/extract-hashtag-text
5
*/
6
 
7
function beliefmedia_hashtags($string) {
8
 
9
 /* Match hashtags */
10
 preg_match_all('/#(\w+)/', $string, $matches);
11
 
12
  /* Add all matches to array */
13
  foreach ($matches[1] as $match) {
14
    $keywords[] = $match;
15
  }
16
 
17
 return (array) $keywords;
18
}
19
 
20
/* Usage */
21
$string = "The quick brown #fox jumped over the lazy #fat#dog";
22
echo '
23
<pre>' . print_r(beliefmedia_hashtags($string), true) . '</pre>
24
 
25
';

Consider the following sentence: "The quick brown #fox jumped over the lazy #fat#dog".

The keywords of fox, fat and dog are our search terms with #fat#dog illustrating the 'unspaced' erroneous use of hashtags.

Using our PHP function we'll return the following array:

1
Array
2
(
3
    [0] => fox
4
    [1] => fat
5
    [2] => dog
6
)

Return Hashtag String

To return the hashtags in a string, use the following:

1
<?php 
2
/*
3
 Extract #Hashtag Keywords from a String of Text with PHP
4
 http://www.beliefmedia.com/extract-hashtag-text
5
*/
6
 
7
function beliefmedia_hashtag_string($string) {
8
 
9
 /* Match hashtags */
10
 preg_match_all('/#(\w+)/', $string, $matches);
11
 
12
  /* Add all matches to array */
13
  foreach ($matches[1] as $match) {
14
    $keywords .= $match . ', ';
15
  }
16
 
17
 return rtrim(trim($keywords), ',');
18
}
19
 
20
/* Usage */
21
$string = "The quick brown #fox jumped over the lazy #fat#dog";
22
echo beliefmedia_hashtag_string($string);

Returns: fox, fat, dog.

Automatically Link Hashtag Words in a Sentence to Search

If you wanted to return the original string of text (not just the hash terms) with hashtag characters linked to a search form, you could use something similar to the following:

1
<?php 
2
/*
3
 Extract #Hashtag Keywords from a String of Text with PHP
4
 http://www.beliefmedia.com/extract-hashtag-text
5
*/
6
 
7
function beliefmedia_hashtag_links($string) {
8
 
9
 preg_match_all('/#(\w+)/', $string, $matches);
10
  foreach ($matches[1] as $match) {
11
    $string = str_replace(&quot;#$match&quot;, &quot;<a href='http://www.beliefmedia.com/?s=$match'>#$match</a>&quot;, &quot;$string&quot;);
12
  }
13
 
14
 return $string;
15
}

Example Usage:

1
<?php 
2
/* Usage */
3
$string = "This is a test using the hashtags of #WordPress and #Shortcode.";
4
echo beliefmedia_hashtag_links($string);

Returns: This is a test using the hashtags of #WordPress and #Shortcode.

Considerations

  • Hashtags should only include alphanumeric characters and underscores. The regular expression '/#(\w+)/' excludes anything other than these characters. The lowercase w matches any alpha numeric character including underscore (_) (while the uppercase W will match any non alpha numeric character excluding the underscore character). A common error on social platforms is to include these illegal characters meaning that the hashtag breaks and loses any significance (you'll often see people incorrectly use apostrophes in hastags).
  • Some social platforms won't permit hashtags that include only numbers... or tags that start with numbers. You may need to alter the expression to suit a particular platform you're trying to emulate.
  • An article scheduled here will show you how to automatically create hashtags from text. It emulates features used by Google+ and others.

Download


Title: Extract Hashtag Keywords from a String of Text with PHP
Description: Extract Hashtag Keywords from a String of Text with PHP. Includes three PHP functions.
  Download • Version 0.2, 664.0B, zip, Category: PHP Code & Snippets

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment