View Full Version : Resolved Give items in foreach ID on name

01-21-2010, 10:46 AM
Here is what I try to do:
- I read a RSS feed in and put the items in separate arrays. (done)
Now here comes the trick:
- When an item contains a certain word it gets an ID.
- This has to be putted into a database. (done)

Like this:


When [item1] contains the word "word1" ID array value is 1.
When [item2] contains the word "word2" ID array value is 2.

This gets into the database:
- INSERT INTO table (itemname, itemlink, ID) VALUES($name, $link, $id)

How do I scan for the word and set the ID value?:confused:

01-21-2010, 01:26 PM
Are you sure you want to create the ID yourself, or let MySQL automatically
create the incremental ID? If you do it yourself, there's a possibility of duplicating
an ID. If MySQL has an "auto-increment" ID, it will never use the same one twice.

Let us know your thoughts on that part first.

01-21-2010, 02:05 PM
It had to become a second ID

AutoID - Itemname - ExtraID

So it has to be something like this:

- Read RSSfeed
- Foreach place in arrays
- if array[1] contains "item1", $ID1 = "1"
- INSERT INTO table (item1, ID1) VALUES($array1, $ID1)
- In database: (autoID, Item1, ID1)

This extra ID is for categorizing.

01-21-2010, 04:22 PM
So the RSS words are in an array ...
How about the other words that it is compared to?
How many of those are there, and is that also an array?

01-21-2010, 04:52 PM
There are 26 words for comparing.
These can be read from a database and put in an array, or already set in arrays.
The words for comparing are static and won't change.

01-21-2010, 06:32 PM
Below is my little test.

If you only have 26 words, I don't think a nested loop will be a problem.
But how many items will be in the RSS array? If there are hundreds or
thousands, then it might be a problem. But this seems the simplest to me.

Maybe someone has a preg_match idea that is more efficient?

Here's what I came up with real quick:
You may get some ideas from it.


// The list of words to compare
$words = array("apple","orange","peach","lime","lemon","banana");

// The RSS array ... for testing.
$rss = array("this is banana test1","this is a test2","this is an apple test3","this is a lime test4","this is apple");

foreach($words as $item1){
foreach($rss as $item2){
echo "<b>".$item1."</b> was found in RSS string (".$i.") ".$item2."<br />";

Here is the output:
apple was found in RSS string (2) this is an apple test3
apple was found in RSS string (4) this is apple
lime was found in RSS string (3) this is a lime test4
banana was found in RSS string (0) this is banana test1


01-21-2010, 06:43 PM
function match_num( $string, $word )
preg_match( '/\b' . preg_quote( $word, '/' ) . '(\d+)\b/i', $string, $matches );
return array_pop( $matches );

var_dump( match_num( 'kbluhm is a loser', 'is' ) ); // NULL

var_dump( match_num( 'kbluhm is2 a loser', 'is' ) ); // string(1) "2"

Something like that?

01-22-2010, 09:52 AM
This is perfect!
Here I can go further with.
I'll post my script here later for use of other scripts:thumbsup:.