1

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);
1
  • Just add the U modifier in existing your pattern and should be fine Commented Jan 20, 2016 at 14:43

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

results in an array. Whats the pipe after the semi colon?
@SantoshPillai A typo... And you want an array I suppose as there are multiple matches.
I am writing only one occurrence and still its an array preg_match_all("/\bIpAddr=(.*?)\|/", $string, $result_array); $result = $result_array[1]; fwrite($file, '$result is ' . result . "\r\n");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.