Link to home
Start Free TrialLog in
Avatar of willsherwood
willsherwood

asked on

PHP REGEX simple extraction

What "search" string expression can i use to extract the INTEGER portion of the second value in the string:
load average: 9.80, 11.58, 13.56

There is text prior and after, but "load average:"  is unique and machine generated

i would like a PHP statement to assign (in the case above) the integer 11

$Load = regex("search expression", $SourceString);

thanks!
SOLUTION
Avatar of ziceva
ziceva
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of willsherwood
willsherwood

ASKER

would this however find integers PRIOR to  "load average"
"load average:"  would be the syntax trigger for the listing of the three decimal values folllowing.
This will find all entities that comply with this 3 rules:
1. have a space before them
2. are all numbers
3. end with a point (.)

The format of the target string is not relevant, except for the case in which the target string starts with the number (this leading number will not get picked up because it doesn't have a space before - this is easily corrected by prepending a space to the target string)

<?
$test="Load average 11.2 14.2 20.1";
preg_match_all ( '/ (([0-9]*)\.)/' , " ".$test , $matches);
print_r($matches[2]);
?>

Open in new window


https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

Please take a moment to read over that article and then give us some good test data.  A single REGEX might not be the best answer, but whatever the answer might be, a few test cases are worth the effort.

load average: 9.80, 11.58, 13.56 // AND WHAT ELSE?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry my problems statement was not clearer,  the second solution works fine.
thanks all!