Returning only the unknown part with preg_match

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Returning only the unknown part with preg_match

Post 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?
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Post 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.
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

Thanks!
Post Reply