Page 1 of 1

Returning only the unknown part with preg_match

Posted: Fri Oct 20, 2006 6:38 am
by Shendemiar
I need to find multiple unknown strings from a long one. I know what is around them, but I dont what they are.

Example:

Code: Select all

ab=KOIRA*kana*hevonen=SIKA*lehmä
Here the KOIRA and SIKA are unknown to me, while the rest i can predict.

I know how to return something like

Code: Select all

ab=KOIRA*kana*
from preg_match, but how do I get only the KOIRA, only the unknown part?

Posted: Fri Oct 20, 2006 6:55 am
by dbevfat
hi,

try using preg_match_all like this:

Code: Select all

$pattern = '/(ab=)(.*)(*kana*hevonen=)(.*)(lehma)/';
preg_match_all($pattern, $yourstring, $matches);
print_r($matches);
you'll see that $matches is an array with parts from the original string. $matches[0] is the whole $yourstring, $matches[1] is the first part "ab=", $matches[2] is the unknown part (KOIRA), and so on.

I'm sure the pattern I wrote is not correct, it's just a prototype to give you an idea.

Posted: Fri Oct 20, 2006 7:14 am
by Shendemiar
Thanks!