Take the 2-minute tour ×

I have a PHP function to show message with hashtag.

PHP code like this :

include 'includes/_db_.php';

$sql_t = "SELECT message FROM t_haps_wall WHERE message LIKE '%#%'";
$hasil_t = mysql_query($sql_t) or die(mysql_error());

while($data_t = mysql_fetch_array($hasil_t))
$message_t = $data_t['message'];

This code is running ok, but now how to set just hashtag show ? If in example like this :

Message = I like you #loveyou

In my current, It will show all message, I just want show #loveyou. Is it possible ?

Thanks for helps.

share|improve this question
Can you define the hashtag? Starts with # and ends with whitespace or end of string? RegEx is what you need here. Also, is this for display purposes only? If it can't be done on the DB end, you can do it in the application for sure, but only if you don't need to sort or group on it or something in the DB. –  Brad Nov 24 '12 at 6:43
Can you have more than one hash in a message? –  Cyclone Nov 24 '12 at 6:44
@KristerAndersson I have more message with 1 or 2 or more hash.. –  Nagini Nov 24 '12 at 6:46
Barmar's answer looks legit, but maybe it will be better to add another hashtags table to your schema, instead of doing string manipulations on the DB server –  yoavmatchulsky Nov 24 '12 at 6:53

1 Answer 1

up vote 5 down vote accepted
SELECT SUBSTR(message, LOCATE('#', message)) hashtag
FROM t_haps_wall
WHERE message like '%#%'
share|improve this answer
thank you very much @barmar. It works. :) –  Nagini Nov 24 '12 at 6:56
@Barmar How will be the output when multiple # occur in the message? –  Nandakumar V Nov 24 '12 at 7:01
It will show everything from the first #. –  Barmar Nov 24 '12 at 7:03

Your Answer


