<?php /** Some strings to test our code */ $testStrings=[ "adress=example.com port=464", "adress =example.com\nport=4645", "port=1 protocol=https adress= example.com", ]; /** Test for each string */ foreach($testStrings as $string) { print "\n\nTESTING:'".escapeshellarg($string)."'\n"; /** First method - does not match them all */ if(preg_match('/\badress=([a-z\.]*)\s+port=(\d*)/',$string,$matches)) { print "Matched {$matches[1]}:{$matches[2]}\n"; } /** Second method - get param/value pairs first, then check them */ if(preg_match_all('/\b(?P<param>[^\d\s]\S*)\s*=\s*(?P<value>\S*)(?:\s|$)/m',$string,$matches)) { //var_dump($matches); // Uncomment to see the raw data. $i=0; // We need an index. foreach($matches['param'] as $param) { $value=$matches['value'][$i]; // Put value in local $value $i++; switch($param) { // Find parameters we know, complain if needed. case "adress": print "ADDRESS IS $value\n"; break; case "port": if(($result=preg_match('/^\d{3}$/',$value))) { var_dump($result); print "PORT IS $value\n"; } else { print "PORT MUST HAVE EXACTLY 3 DIGITS - Got: $value\n"; } break; default: print "UNKNOWN PARAMETER $param=$value\n"; } } } }
You have javascript disabled. You will not be able to edit any code.