PREG_MATCH and arrays

PREG_MATCH and arrays

am 02.01.2008 21:49:59 von Poster

I am trying to pass an array of titles through an array of "keywords". The
$filter_test will later be replaced with real data coming from another
source, I am testing it this way to save time.

I have this:

$filter_test = array("Thundarr the Barbarian", "Cash and Treasure", "Welcome
Back Kotter", "Amazing Race", "The Waltons");
$filter_title = array("Thundarr", "Cash", "Kotter");

foreach($filter_test as $valtest) {
foreach($filter_title as $val) {
if (preg_match("/\b" . $val . "\b/i", $valtest)) {
echo "MATCH " . $valtest . "\n";
} else {
echo "NO MATCH " . $valtest . "\n";
}
}
}

The problem is, this is what is returned:

MATCH Thundarr the Barbarian
NO MATCH Thundarr the Barbarian
NO MATCH Thundarr the Barbarian
NO MATCH Cash and Treasure
MATCH Cash and Treasure
NO MATCH Cash and Treasure
NO MATCH Welcome Back Kotter
NO MATCH Welcome Back Kotter
MATCH Welcome Back Kotter
NO MATCH Amazing Race
NO MATCH Amazing Race
NO MATCH Amazing Race
NO MATCH The Waltons
NO MATCH The Waltons
NO MATCH The Waltons

Why does it only match on the first, then no match on the other identical
entries ? Please go easy, this is my attempt at both arrays and preg_match.

Re: PREG_MATCH and arrays

am 02.01.2008 22:28:13 von luiheidsgoeroe

On Wed, 02 Jan 2008 21:49:59 +0100, Poster wrote:

> I am trying to pass an array of titles through an array of "keywords".=
=

> The $filter_test will later be replaced with real data coming from =

> another source, I am testing it this way to save time.
>
> I have this:
>
> $filter_test =3D array("Thundarr the Barbarian", "Cash and Treasure", =
=

> "Welcome Back Kotter", "Amazing Race", "The Waltons");
> $filter_title =3D array("Thundarr", "Cash", "Kotter");
>
> foreach($filter_test as $valtest) {
> foreach($filter_title as $val) {
> if (preg_match("/\b" . $val . "\b/i", $valtest)) {
> echo "MATCH " . $valtest . "\n";
> } else {
> echo "NO MATCH " . $valtest . "\n";
> }
> }
> }
>
> The problem is, this is what is returned:
>
> MATCH Thundarr the Barbarian

'Thundar' matches 'Thundar the Barbarian'

> NO MATCH Thundarr the Barbarian

'Cash' does not match 'Thundarr the Barbarian'

> NO MATCH Thundarr the Barbarian

'Kotter' does not match 'Thundarr the Barbarian'

> NO MATCH Cash and Treasure

'Thundar' does not mathc 'Cash and treasure'

etc. etc, no problem there.

> Why does it only match on the first, then no match on the other =

> identical entries ? Please go easy, this is my attempt at both arrays =
=

> and preg_match.

Keep in mind the inner most foreach will be run entirely before the seco=
nd =

entry of the outer foreach.
-- =

Rik Wasmus

Re: PREG_MATCH and arrays

am 02.01.2008 22:37:36 von Michael Fesser

..oO(Poster)

>I am trying to pass an array of titles through an array of "keywords". The
>$filter_test will later be replaced with real data coming from another
>source, I am testing it this way to save time.
>
>I have this:
>[...]
>
>Why does it only match on the first, then no match on the other identical
>entries ? Please go easy, this is my attempt at both arrays and preg_match.

$filter_test = array("Thundarr the Barbarian", "Cash and Treasure",
"Welcome
Back Kotter", "Amazing Race", "The Waltons");
$filter_title = array("Thundarr", "Cash", "Kotter");

foreach($filter_test as $valtest) {
foreach($filter_title as $val) {
if (preg_match("/\b$val\b/i", $valtest)) {
echo "MATCH '$val' ON '$valtest'\n";
} else {
echo "NO MATCH '$val' ON '$valtest'\n";
}
}
}

Now clearer?

Micha

Re: PREG_MATCH and arrays

am 02.01.2008 22:49:54 von Poster

"Rik Wasmus" wrote in message
news:op.t4bm9bgf5bnjuv@metallium.lan...
On Wed, 02 Jan 2008 21:49:59 +0100, Poster wrote:

> I am trying to pass an array of titles through an array of "keywords".
> The $filter_test will later be replaced with real data coming from
> another source, I am testing it this way to save time.
>
> I have this:
>
> $filter_test = array("Thundarr the Barbarian", "Cash and Treasure",
> "Welcome Back Kotter", "Amazing Race", "The Waltons");
> $filter_title = array("Thundarr", "Cash", "Kotter");
>
> foreach($filter_test as $valtest) {
> foreach($filter_title as $val) {
> if (preg_match("/\b" . $val . "\b/i", $valtest)) {
> echo "MATCH " . $valtest . "\n";
> } else {
> echo "NO MATCH " . $valtest . "\n";
> }
> }
> }
>
> The problem is, this is what is returned:
>
> MATCH Thundarr the Barbarian

'Thundar' matches 'Thundar the Barbarian'

> NO MATCH Thundarr the Barbarian

'Cash' does not match 'Thundarr the Barbarian'

> NO MATCH Thundarr the Barbarian

'Kotter' does not match 'Thundarr the Barbarian'

> NO MATCH Cash and Treasure

'Thundar' does not mathc 'Cash and treasure'

etc. etc, no problem there.

> Why does it only match on the first, then no match on the other identical
> entries ? Please go easy, this is my attempt at both arrays and
> preg_match.

Keep in mind the inner most foreach will be run entirely before the second
entry of the outer foreach.
--
Rik Wasmus

Thanks, I didn't realize that. I'll have to rethink the checking process
now.

Re: PREG_MATCH and arrays

am 02.01.2008 23:00:05 von Michael Fesser

..oO(Poster)

>"Rik Wasmus" wrote in message
>news:op.t4bm9bgf5bnjuv@metallium.lan...
>
>>Keep in mind the inner most foreach will be run entirely before the second
>>entry of the outer foreach.
>
>Thanks, I didn't realize that. I'll have to rethink the checking process
>now.

You could concatenate all your keywords with an '|', which will be seen
as a logical OR in the pattern. Then just drop the inner loop:

$filter_test = array("Thundarr the Barbarian", ...);
$filter_title = array("Thundarr", "Cash", "Kotter");

$keywords = implode('|', $filter_title);

foreach ($filter_test as $title) {
if (preg_match("/\b$keywords\b/i", $title)) {
// match
} else {
// no match
}
}

Or something like that.

Micha

Re: PREG_MATCH and arrays

am 02.01.2008 23:04:22 von luiheidsgoeroe

On Wed, 02 Jan 2008 23:00:05 +0100, Michael Fesser wrot=
e:

> .oO(Poster)
>
>> "Rik Wasmus" wrote in message
>> news:op.t4bm9bgf5bnjuv@metallium.lan...
>>
>>> Keep in mind the inner most foreach will be run entirely before the =
=

>>> second
>>> entry of the outer foreach.
>>
>> Thanks, I didn't realize that. I'll have to rethink the checking proc=
ess
>> now.
>
> You could concatenate all your keywords with an '|', which will be see=
n
> as a logical OR in the pattern. Then just drop the inner loop:
>
> $filter_test =3D array("Thundarr the Barbarian", ...);
> $filter_title =3D array("Thundarr", "Cash", "Kotter");
>
> $keywords =3D implode('|', $filter_title);
>
> foreach ($filter_test as $title) {
> if (preg_match("/\b$keywords\b/i", $title)) {
> // match
> } else {
> // no match
> }
> }
>
> Or something like that.

One could do that, I'd advise top use a preg_quote() on the values thoug=
h.
-- =

Rik Wasmus

Re: PREG_MATCH and arrays

am 02.01.2008 23:14:42 von Poster

"Michael Fesser" wrote in message
news:n22on3p0mumpluo6oemh3ig4bcoaq1mla8@4ax.com...
> .oO(Poster)
>
>>"Rik Wasmus" wrote in message
>>news:op.t4bm9bgf5bnjuv@metallium.lan...
>>
>>>Keep in mind the inner most foreach will be run entirely before the
>>>second
>>>entry of the outer foreach.
>>
>>Thanks, I didn't realize that. I'll have to rethink the checking process
>>now.
>
> You could concatenate all your keywords with an '|', which will be seen
> as a logical OR in the pattern. Then just drop the inner loop:
>
> $filter_test = array("Thundarr the Barbarian", ...);
> $filter_title = array("Thundarr", "Cash", "Kotter");
>
> $keywords = implode('|', $filter_title);
>
> foreach ($filter_test as $title) {
> if (preg_match("/\b$keywords\b/i", $title)) {
> // match
> } else {
> // no match
> }
> }
>
> Or something like that.
>
> Micha

That worked perfect !!! Thanks (and it makes sense - even better).

Re: PREG_MATCH and arrays

am 02.01.2008 23:15:36 von Poster

"Rik Wasmus" wrote in message
news:op.t4boxkyc5bnjuv@metallium.lan...
On Wed, 02 Jan 2008 23:00:05 +0100, Michael Fesser wrote:

> .oO(Poster)
>
>> "Rik Wasmus" wrote in message
>> news:op.t4bm9bgf5bnjuv@metallium.lan...
>>
>>> Keep in mind the inner most foreach will be run entirely before the
>>> second
>>> entry of the outer foreach.
>>
>> Thanks, I didn't realize that. I'll have to rethink the checking process
>> now.
>
> You could concatenate all your keywords with an '|', which will be seen
> as a logical OR in the pattern. Then just drop the inner loop:
>
> $filter_test = array("Thundarr the Barbarian", ...);
> $filter_title = array("Thundarr", "Cash", "Kotter");
>
> $keywords = implode('|', $filter_title);
>
> foreach ($filter_test as $title) {
> if (preg_match("/\b$keywords\b/i", $title)) {
> // match
> } else {
> // no match
> }
> }
>
> Or something like that.

One could do that, I'd advise top use a preg_quote() on the values though.
--
Rik Wasmus

Thanks for your posts too - I'll look on the PHP site about the preg_quote.
There are so many functions to check out. Thanks for your help.

Re: PREG_MATCH and arrays

am 02.01.2008 23:29:17 von Michael Fesser

..oO(Rik Wasmus)

>On Wed, 02 Jan 2008 23:00:05 +0100, Michael Fesser wrote:
>
>> $keywords = implode('|', $filter_title);
>>
>> foreach ($filter_test as $title) {
>> if (preg_match("/\b$keywords\b/i", $title)) {
>> // match
>> } else {
>> // no match
>> }
>> }
>>
>> Or something like that.
>
>One could do that, I'd advise top use a preg_quote() on the values though.

Good point.

Micha