Hi all,

What I am trying to accomplish is this:

  1. User submits form
  2. PHP checks the form for any words that begin with the '#' hash symbol
  3. If any words beginning with # are found, php checks database to see if it already exists

What is the best way to do this?

Please keep in mind that the form being checked for these words will contain up to 200 character, and may contain more than one hashtagged word.

I imagine that the flow would look vaguely like this:

form submitted > check for #hashtagged words > put those words in an array > loop through array and check DB if it already exists > return results to user

The step from above that I need help on is finding the words and putting them into an array.

I've managed to figure out how to convert those words to links using preg_replace. In case it helps anyone, here it is below:

preg_replace('/(#\w+)/', '<a href=\'http://yourwebsite.com/script.php?get=\1\'>\1</a>', $string);

Thanks in advance for your time!

3 Years
Discussion Span
Last Post by diafol

Hi paulkd

I will be using this to process several different forms that a user would potentially be submitting.
In one form (the status update), it would be the only field, but there are other forms that that may have a few additional fields (however, if it matters, in those forms, I may be able to simply autofill the values with things like session variables).


and are the hashtags in your database in a single column?

e.g. hashtags varchar(200) NOT NULL

so the sql would be something like:-

select count(hashtag) from hashtags where hashtag = $thisHashTag


Yep, that is correct.
The result of the query
select count(hashtag) from hashtags where hashtag = $thisHashTag

would result in '1'

or if the query were
select hashtag from hashtags where hashtag = $thisHashTag

the result would be #ExampleHashTag

//simulate posted field
$_POST['formfield']='Is #star contained in your #database of #hashtags?';


if($matches[0]) {
    foreach($matches[0] as $hashtag) {
        $sql = "select hashtag from `hashtag-table` where hashtag = '".substr($hashtag,1)."'";
        $result = mysql_query($sql);
        echo mysql_num_rows($result) ? $hashtag.' found<br>' : $hashtag.' not found<br>';

Sorry everyone it's mysql - been using CI query builder recently so haven't gotten around to the mysqli syntax, or even PDO.

Edited by paulkd: added a couple of ticks


paulkd, that worked beautifully!

Just in case this helps anyone else in the future who may read this, and it doesn't work for you --

The only modification to paulkd's script above that was needed (other than providing your database table, of course), was to replace the substr($hashtag,1) part with simply $hashtag
The reason for that is because in my database, the hashtags are stored with the hash symbol (#) included in the data fields. (paulkd's script removes the hashtag before querying the database. So instead of searching for '#example', it would search for 'example' ... your database setup may differ from mine, so try both if one method doesnt work for you)

Thank you very much for your time and help, paulkd!

Edited by arout77


Not sure how to mark thread as solved, dont see any options for it, but please consider this solved (or mods can mark it for me)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.