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!

Recommended Answers

All 8 Replies

Is there more than one field in the form?

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?';

preg_match_all('/#\w+/',$_POST['formfield'],$matches);

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.

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!

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)

Member Avatar for diafol

Ok done - marked it solved before I wrote this. I'm no 'solved monster' ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.