Santosh Pillai
Santosh Pillai

Reputation: 1391

php preg match between two strings

How do I get the IpAddr values from this string below using preg match

{{Id=0|IpAddr=172.28.190.48|gateway=172.17.00.01|mask=255.255.255.240|pref=1}|{Id=0|IpAddr=172.30.228.64|gateway=172.17.01.02|mask=255.255.255.192|pref=1}|{Id=0|IpAddr=0.0.0.0|gateway=172.19.00.01|mask=0.0.0.0|pref=1}}

I tried below but it does not give results

preg_match_all("/.*IpAddr=(.*)\|/", $string, $result_array);

Upvotes: 1

Views: 68

Answers (1)

jeroen
jeroen

Reputation: 91792

You are probably matching to much and getting only 1 result, you would need lazy / ungreedy matching:

preg_match_all("/\bIpAddr=(.*?)\|/", $string, $result_array);
                             ^ Lazy / ungreedy match, take as few as possible
                 ^^ a word boundary before the IpAddr string

Upvotes: 2

Related Questions