View Full Version : Generating Words PHP

05-24-2011, 01:06 PM
Hi im looking to make some kind of match thing were 2 people recieve similar emails. In the email they will both be given a random word. By random I mean 'Banana' or 'Turqoise' etc

How do I do this?
My initial guess is to create a random number and pick the word out of a list at this corresponding number. How would I compile the list and what platform would i make it on? I am new to all this stuff :s

Thanks guys

05-24-2011, 04:22 PM
This project could be done a number of ways.

You could use a database (MySql being a very popular choice) to store your desired words. Then, yes, you would pluck them from the database by a randomly selected key or by using something like SELECT * FROM `table` ORDER BY RAND() LIMIT 0,1; (if your database isn't enormous).

Otherwise, you could skip the database and just use a PHP array variable to store your words and select a random entry from that array.

As for compiling the list in the first place... well that's up to you.

05-24-2011, 06:27 PM
Thanks for that! On the MySQL point I was wondering if there was something else you may be able to give me a quick view on...

I wanted to match names in a table. Thing is users input different names. I understand I can use % to match Jo% with Jo, Joe and Joseph, but is there any other ways to match words that may not be exact but very similar?

So far I have only got words down to lowercase to get better matching, but I wanted to match say "Rick Smith" to "Rich Smith", the jist been names maybe slightly different but out of say 5,000 people chances are the match is correct, as the users may have entered a name slightly differently.


05-24-2011, 06:46 PM
Hmm. Well if you're storing the first and last names in different columns of the table then you would just do a wildcard on each one (or on one but not the other, or whatever you want to do).

Something like:

SELECT * FROM `table` WHERE first_name LIKE 'Ri%' AND last_name LIKE 'Smi%';


SELECT * FROM `table` WHERE first_name LIKE 'Ri%' AND last_name = 'Smith';

Otherwise you're looking at using "regular expressions" (check google if you're not familiar with these) to get the job done. Those can be pretty sophisticated but also pretty difficult to create. There is MySql syntax for this:


Regular expressions are pretty neat though.