Welcome to MobyThreads.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in
All support for the MobyThreads Threaded phpBB MOD can now be found on welsolutions at this forum

Simple PHP ? - Match words in a string

 
   Web Hosting and Web Master Forums (Home) -> Webmaster RSS
Next:  Review on article on website design required  
Author Message
null

External


Since: Jun 28, 2003
Posts: 18



(Msg. 1) Posted: Sun Jun 29, 2003 2:06 am
Post subject: Simple PHP ? - Match words in a string
Archived from groups: alt>www>webmaster (more info?)

This is a simple one.

I have a string a few hundred characters long being fed to a php script.

I want the script to break down the string into individual words, then
check them for matches against a MySQL database. The final step is to
print the matched words with a description from the database.

The database I can do, but I'm having a problem getting the script to
recognise words from the original string.

Any code snippets or tutorials would be greatly appriciated!

Cheers,
- Dan

 >> Stay informed about: Simple PHP ? - Match words in a string 
Back to top
Login to vote
postmaster

External


Since: Jun 27, 2003
Posts: 286



(Msg. 2) Posted: Sun Jun 29, 2003 4:15 am
Post subject: Re: Simple PHP ? - Match words in a string [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Daniel Ruscoe wrote:

 > This is a simple one.
 >
 > I have a string a few hundred characters long being fed to a php script.
 >
 > I want the script to break down the string into individual words, then
 > check them for matches against a MySQL database. The final step is to
 > print the matched words with a description from the database.
 >
 > The database I can do, but I'm having a problem getting the script to
 > recognise words from the original string.
 >
 > Any code snippets or tutorials would be greatly appriciated!
 >
 > Cheers,
 > - Dan

check out the explode function.

John

--
email: mail(at)johnbokma.com (or reply) home: <a style='text-decoration: underline;' href="http://johnbokma.com/" target="_blank">http://johnbokma.com/</a>
website design tips: <a style='text-decoration: underline;' href="http://johnbokma.com/websitedesign/" target="_blank">http://johnbokma.com/websitedesign/</a> (preliminary)<!-- ~MESSAGE_AFTER~ -->

 >> Stay informed about: Simple PHP ? - Match words in a string 
Back to top
Login to vote
null

External


Since: Jun 28, 2003
Posts: 18



(Msg. 3) Posted: Sun Jun 29, 2003 4:15 am
Post subject: Re: Simple PHP ? - Match words in a string [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <1056842111.207624.RemoveThis@halkan.kabelfoon.nl>, John Bokma says...
 > Daniel Ruscoe wrote:
 >
  > > This is a simple one.
  > >
  > > I have a string a few hundred characters long being fed to a php script.
[..]
  > > The database I can do, but I'm having a problem getting the script to
  > > recognise words from the original string.
[..]
 > check out the explode function.

That's exactly what I'm stuck on Smile

I'll write bit of inaccurate code to show what I'm thinking:

$tablename = animals_in_the_zoo;

$animal_search = "lion tiger zebra monkey football-hooligan"
$animals = explode(" ", $animal_search);

So we've broken up the original string, but still need to match it to the
database. This isn't done properly, but...

select * from $tablename where animal = $animals[0];

Then printing the result of that query will show up any matches to "lion"
in the table "animals_in_the_zoo". I think.

So how would I go about executing that query for every string, uping the
number in the [] by one each time?

Thanks for the help. With any luck, I'll get this cracked tonight Smile

- Dan<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Simple PHP ? - Match words in a string 
Back to top
Login to vote
spamtrap1

External


Since: Jun 28, 2003
Posts: 2



(Msg. 4) Posted: Sun Jun 29, 2003 4:15 am
Post subject: Re: Simple PHP ? - Match words in a string [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

With total disregard for any kind of safety measures Daniel Ruscoe
<null.DeleteThis@null.null> leapt forth and uttered:

 > That's exactly what I'm stuck on Smile
 >
 > I'll write bit of inaccurate code to show what I'm thinking:
 >
 > $tablename = animals_in_the_zoo;
 >
 > $animal_search = "lion tiger zebra monkey football-hooligan"
 > $animals = explode(" ", $animal_search);
 >
 > So we've broken up the original string, but still need to match
 > it to the database. This isn't done properly, but...
 >
 > select * from $tablename where animal = $animals[0];
 >
 > Then printing the result of that query will show up any matches
 > to "lion" in the table "animals_in_the_zoo". I think.
 >
 > So how would I go about executing that query for every string,
 > uping the number in the [] by one each time?
 >
 > Thanks for the help. With any luck, I'll get this cracked
 > tonight Smile
 >
 > - Dan

Hows about tring:

"SELECT FROM $tablename WHERE anaimal LIKE %$animals[0]%";

--
There is no signature.....<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Simple PHP ? - Match words in a string 
Back to top
Login to vote
postmaster

External


Since: Jun 27, 2003
Posts: 286



(Msg. 5) Posted: Sun Jun 29, 2003 5:11 am
Post subject: Re: Simple PHP ? - Match words in a string [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Daniel Ruscoe wrote:

 > In article <1056842111.207624.TakeThisOut@halkan.kabelfoon.nl>, John Bokma says...
 >
  >>Daniel Ruscoe wrote:
  >>
  >>
   >>>This is a simple one.
   >>>
   >>>I have a string a few hundred characters long being fed to a php script.
 >
 > [..]
 >
   >>>The database I can do, but I'm having a problem getting the script to
   >>>recognise words from the original string.
 >
 > [..]
 >
  >>check out the explode function.
 >
 >
 > That's exactly what I'm stuck on Smile
 >
 > I'll write bit of inaccurate code to show what I'm thinking:
 >
 > $tablename = animals_in_the_zoo;
 >
 > $animal_search = "lion tiger zebra monkey football-hooligan"
 > $animals = explode(" ", $animal_search);
 >
 > So we've broken up the original string, but still need to match it to the
 > database. This isn't done properly, but...
 >
 > select * from $tablename where animal = $animals[0];
 >
 > Then printing the result of that query will show up any matches to "lion"
 > in the table "animals_in_the_zoo". I think.
 >
 > So how would I go about executing that query for every string, uping the
 > number in the [] by one each time?

for ($index = 0; $index < $animals; $index++) {

select... $animals[$index];

}

I no zero to nothing about PHP (but I know Perl Smile. It is also possible
to build the query with: animal = $animals[0] OR animal = $animals[1]
.... (with join if PHP supports that) and do the query in one go. It is
faster and less load on the database (I think)

John

--
email: mail(at)johnbokma.com (or reply) home: <a style='text-decoration: underline;' href="http://johnbokma.com/" target="_blank">http://johnbokma.com/</a>
website design tips: <a style='text-decoration: underline;' href="http://johnbokma.com/websitedesign/" target="_blank">http://johnbokma.com/websitedesign/</a> (preliminary)<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Simple PHP ? - Match words in a string 
Back to top
Login to vote
davidvb

External


Since: Jun 28, 2003
Posts: 114



(Msg. 6) Posted: Sun Jun 29, 2003 2:06 pm
Post subject: Re: Simple PHP ? - Match words in a string [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Daniel Ruscoe wrote:

 > In article <1056842111.207624.DeleteThis@halkan.kabelfoon.nl>, John Bokma says...
 >
  >>Daniel Ruscoe wrote:
  >>
  >>
   >>>This is a simple one.
   >>>
   >>>I have a string a few hundred characters long being fed to a php script.
 >
 > [..]
 >
   >>>The database I can do, but I'm having a problem getting the script to
   >>>recognise words from the original string.
 >
 > [..]
 >
  >>check out the explode function.
 >
 >
 > That's exactly what I'm stuck on Smile
 >
 > I'll write bit of inaccurate code to show what I'm thinking:
 >
 > $tablename = animals_in_the_zoo;
 >
 > $animal_search = "lion tiger zebra monkey football-hooligan"
 > $animals = explode(" ", $animal_search);
 >
 > So we've broken up the original string, but still need to match it to the
 > database. This isn't done properly, but...
 >
 > select * from $tablename where animal = $animals[0];
 >
 > Then printing the result of that query will show up any matches to "lion"
 > in the table "animals_in_the_zoo". I think.
 >
 > So how would I go about executing that query for every string, uping the
 > number in the [] by one each time?
 >
 > Thanks for the help. With any luck, I'll get this cracked tonight Smile

This, perhaps?

Using in_array():
<a style='text-decoration: underline;' href="http://au.php.net/manual/en/function.in-array.php" target="_blank">http://au.php.net/manual/en/function.in-array.php</a>

<?php
// To get words
$animal_search = "lion tiger zebra monkey football-hooligan"
$animals = explode(" ", $animal_search);

// Set up sql
$sql = "SELECT * FROM $tablename";

$result = mysql_query($sql,$cnx)
or die("Couldn't execute sql query.");

// Process sql against words
while ($row = @mysql_fetch_array($result)) {
  if (in_array($row,$animals)) {
  // Yes, it is in the database. Now do stuff!
  break 1;
  }
  else {
  // Continue on, ye lost traveller.
  }
}
?>

HTH<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Simple PHP ? - Match words in a string 
Back to top
Login to vote
postmaster

External


Since: Jun 27, 2003
Posts: 286



(Msg. 7) Posted: Sun Jun 29, 2003 2:06 pm
Post subject: Re: Simple PHP ? - Match words in a string [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

David Venn-Brown wrote:
 > Daniel Ruscoe wrote:
 >
  >> In article <1056842111.207624 RemoveThis @halkan.kabelfoon.nl>, John Bokma says...
  >>
   >>> Daniel Ruscoe wrote:
   >>>
   >>>
   >>>> This is a simple one.
   >>>>
   >>>> I have a string a few hundred characters long being fed to a php
   >>>> script.
  >>
  >>
  >> [..]
  >>
   >>>> The database I can do, but I'm having a problem getting the script
   >>>> to recognise words from the original string.
  >>
  >>
  >> [..]
  >>
   >>> check out the explode function.
  >>
  >>
  >>
  >> That's exactly what I'm stuck on Smile
  >>
  >> I'll write bit of inaccurate code to show what I'm thinking:
  >>
  >> $tablename = animals_in_the_zoo;
  >>
  >> $animal_search = "lion tiger zebra monkey football-hooligan"
  >> $animals = explode(" ", $animal_search);
  >>
  >> So we've broken up the original string, but still need to match it to
  >> the database. This isn't done properly, but...
  >>
  >> select * from $tablename where animal = $animals[0];
  >>
  >> Then printing the result of that query will show up any matches to
  >> "lion" in the table "animals_in_the_zoo". I think.
  >>
  >> So how would I go about executing that query for every string, uping
  >> the number in the [] by one each time?
  >>
  >> Thanks for the help. With any luck, I'll get this cracked tonight Smile
 >
 >
 > This, perhaps?
 >
 > Using in_array():
<font color=purple> > <a style='text-decoration: underline;' href="http://au.php.net/manual/en/function.in-array.php</font" target="_blank">http://au.php.net/manual/en/function.in-array.php</font</a>>
 >
 > <?php
 > // To get words
 > $animal_search = "lion tiger zebra monkey football-hooligan"
 > $animals = explode(" ", $animal_search);
 >
 > // Set up sql
 > $sql = "SELECT * FROM $tablename";

oops, you mean pulling *all* rows from the database??

 > $result = mysql_query($sql,$cnx)
 > or die("Couldn't execute sql query.");
 >
 > // Process sql against words
 > while ($row = @mysql_fetch_array($result)) {
 > if (in_array($row,$animals)) {

^^^^^^^^^^^^^^^^ and testing here?

Let the database do *as much as possible*. It is specialised in data
handling. Imagine pulling 100.000 records from the database and
*testing* them in PHP...

John

--
email: mail(at)johnbokma.com (or reply) home: <a style='text-decoration: underline;' href="http://johnbokma.com/" target="_blank">http://johnbokma.com/</a>
website design tips: <a style='text-decoration: underline;' href="http://johnbokma.com/websitedesign/" target="_blank">http://johnbokma.com/websitedesign/</a> (preliminary)<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Simple PHP ? - Match words in a string 
Back to top
Login to vote
davidvb

External


Since: Jun 28, 2003
Posts: 114



(Msg. 8) Posted: Sun Jun 29, 2003 3:47 pm
Post subject: Re: Simple PHP ? - Match words in a string [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

John Bokma wrote:

 > David Venn-Brown wrote:
 >
  >> Daniel Ruscoe wrote:
  >>
   >>> In article <1056842111.207624.TakeThisOut@halkan.kabelfoon.nl>, John Bokma says...
   >>>
   >>>> Daniel Ruscoe wrote:
   >>>>
   >>>>
   >>>>> This is a simple one.
   >>>>>
   >>>>> I have a string a few hundred characters long being fed to a php
   >>>>> script.
   >>>
   >>>
   >>>
   >>> [..]
   >>>
   >>>>> The database I can do, but I'm having a problem getting the script
   >>>>> to recognise words from the original string.
   >>>
   >>>
   >>>
   >>> [..]
   >>>
   >>>> check out the explode function.
   >>>
   >>>
   >>>
   >>>
   >>> That's exactly what I'm stuck on Smile
   >>>
   >>> I'll write bit of inaccurate code to show what I'm thinking:
   >>>
   >>> $tablename = animals_in_the_zoo;
   >>>
   >>> $animal_search = "lion tiger zebra monkey football-hooligan"
   >>> $animals = explode(" ", $animal_search);
   >>>
   >>> So we've broken up the original string, but still need to match it to
   >>> the database. This isn't done properly, but...
   >>>
   >>> select * from $tablename where animal = $animals[0];
   >>>
   >>> Then printing the result of that query will show up any matches to
   >>> "lion" in the table "animals_in_the_zoo". I think.
   >>>
   >>> So how would I go about executing that query for every string, uping
   >>> the number in the [] by one each time?
   >>>
   >>> Thanks for the help. With any luck, I'll get this cracked tonight Smile
  >>
  >>
  >>
  >> This, perhaps?
  >>
  >> Using in_array():
<font color=green>  >> <a style='text-decoration: underline;' href="http://au.php.net/manual/en/function.in-array.php</font" target="_blank">http://au.php.net/manual/en/function.in-array.php</font</a>>
  >>
  >> <?php
  >> // To get words
  >> $animal_search = "lion tiger zebra monkey football-hooligan"
  >> $animals = explode(" ", $animal_search);
  >>
  >> // Set up sql
  >> $sql = "SELECT * FROM $tablename";
 >
 >
 > oops, you mean pulling *all* rows from the database??
 >
  >> $result = mysql_query($sql,$cnx)
  >> or die("Couldn't execute sql query.");
  >>
  >> // Process sql against words
  >> while ($row = @mysql_fetch_array($result)) {
  >> if (in_array($row,$animals)) {
 >
 >
 > ^^^^^^^^^^^^^^^^ and testing here?
 >
 > Let the database do *as much as possible*. It is specialised in data
 > handling. Imagine pulling 100.000 records from the database and
 > *testing* them in PHP...

It's simply another option. For a small number of records, it would
probably be faster because there is only one call to the database.

Depending on what Dan wants to do, this could be useful. No need to
flame me for it...<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Simple PHP ? - Match words in a string 
Back to top
Login to vote
user104

External


Since: Jun 28, 2003
Posts: 1662



(Msg. 9) Posted: Sun Jun 29, 2003 6:42 pm
Post subject: Re: Simple PHP ? - Match words in a string [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Daniel Ruscoe" <null RemoveThis @null.null> wrote in message
news:MPG.19683c9dad2eff75989687@news.btopenworld.com...

 > $animal_search = "lion tiger zebra monkey football-hooligan"

There's a mistake in your code Daniel.

"football-hooligan" should be "England-fan"

<gets coat>
--
Charles Sweeney
<a style='text-decoration: underline;' href="http://www.CharlesSweeney.com" target="_blank">www.CharlesSweeney.com</a><!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Simple PHP ? - Match words in a string 
Back to top
Login to vote
www

External


Since: Jun 29, 2003
Posts: 720



(Msg. 10) Posted: Sun Jun 29, 2003 7:50 pm
Post subject: Re: Simple PHP ? - Match words in a string [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Sun, 29 Jun 2003 15:42:10 +0100 "Charles Sweeney"
<me.DeleteThis@charlessweeney.com> broke off from drinking a cup of tea at to
write:

 >"Daniel Ruscoe" <null.DeleteThis@null.null> wrote in message
 >news:MPG.19683c9dad2eff75989687@news.btopenworld.com...
 >
  >> $animal_search = "lion tiger zebra monkey football-hooligan"
 >
 >There's a mistake in your code Daniel.
 >
 >"football-hooligan" should be "England-fan"
 >
 ><gets coat>

No need to get your coat. That was a fair comment.

Matt

--
A massive matrix of concise, interlinked encyclopaedia information.
For when you just want to know, quickly and easily.
<a style='text-decoration: underline;' href="http://www.probertencyclopaedia.com" target="_blank">http://www.probertencyclopaedia.com</a><!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Simple PHP ? - Match words in a string 
Back to top
Login to vote
eightninethree

External


Since: Jun 28, 2003
Posts: 171



(Msg. 11) Posted: Sun Jun 29, 2003 7:50 pm
Post subject: Re: Simple PHP ? - Match words in a string [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Matt Probert" <www DeleteThis @probertencyclopaedia.com> wrote in message
news:3eff18bb.94953156@news.ntlworld.com...
 > On Sun, 29 Jun 2003 15:42:10 +0100 "Charles Sweeney"
 > <me DeleteThis @charlessweeney.com> broke off from drinking a cup of tea at to
 > write:
 >
  > >"Daniel Ruscoe" <null DeleteThis @null.null> wrote in message
  > >news:MPG.19683c9dad2eff75989687@news.btopenworld.com...
  > >
   > >> $animal_search = "lion tiger zebra monkey football-hooligan"
  > >
  > >There's a mistake in your code Daniel.
  > >
  > >"football-hooligan" should be "England-fan"
  > >
  > ><gets coat>
 >
 > No need to get your coat. That was a fair comment.
 >
 > Matt
 >

#1 Google result for the word "Football" - <a style='text-decoration: underline;' href="http://nfl.com/" target="_blank">http://nfl.com/</a>

'nuff said!

<grabs keys>


--
Karl Core

Charles Sweeney says my sig is fine as it is.<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Simple PHP ? - Match words in a string 
Back to top
Login to vote
ngx

External


Since: Jun 28, 2003
Posts: 578



(Msg. 12) Posted: Mon Jun 30, 2003 1:00 am
Post subject: Re: Simple PHP ? - Match words in a string [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Matt Probert wrote:
 > On Sun, 29 Jun 2003 15:42:10 +0100 "Charles Sweeney"
 > <me.TakeThisOut@charlessweeney.com> broke off from drinking a cup of tea at to
 > write:
 >
  >> "Daniel Ruscoe" <null.TakeThisOut@null.null> wrote in message
  >> news:MPG.19683c9dad2eff75989687@news.btopenworld.com...
  >>
   >>> $animal_search = "lion tiger zebra monkey football-hooligan"
  >>
  >> There's a mistake in your code Daniel.
  >>
  >> "football-hooligan" should be "England-fan"
  >>
  >> <gets coat>
 >
 > No need to get your coat. That was a fair comment.
 >

No it wasn't. Funny - yes. In the way that picking out any stereotype is
funny. I strongly object to the association between fans of any
sport/event/person/place being confused with the perpetrators of mindless
violence and intimidation except within the context of humour.

"Not in my name" is an expression which has been given a good airing in
recent times. It is appropriate here. Thugs may be wearing my colours but
they do not represent me or my clan/group/party/society.

Furthermore, I am not responsible for them or their actions (in the same way
that you are not responsible for any spam that pretends to come from one of
your domains) nor do I give a hoot about the severity of their treatment
when apprehended and convicted in your juristriction.

But please, whether they are fans or not is irrelevant - remember Barry
George was a big fan of Jill Dando.
--
William Tasso - <a style='text-decoration: underline;' href="http://www.WilliamTasso.com" target="_blank">http://www.WilliamTasso.com</a><!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Simple PHP ? - Match words in a string 
Back to top
Login to vote
Display posts from previous:   
   Web Hosting and Web Master Forums (Home) -> Webmaster All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]