Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

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!
0
willsherwood
Asked:
willsherwood
  • 2
  • 2
  • 2
2 Solutions
 
zicevaCommented:
<?

$test="Load average 11.2 14.2 20.1";

preg_match_all ( '/ (([0-9]*)\.)/' , $test , $matches);

print_r($matches[2]);

?>
0
 
willsherwoodAuthor Commented:
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.
0
 
zicevaCommented:
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


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
http://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?
0
 
Ray PaseurCommented:
http://www.laprbass.com/RAY_temp_willsherwood.php
Outputs:
11
12
0
-3
<?php // RAY_temp_willsherwood.php
error_reporting(E_ALL);
echo "<pre>";

// SOME TEST DATA - IS THIS REPRESENTATIVE OF THE ACTUAL DATA?
$things = array
( 'random load average: 9.80, 11.58, 13.56 '
, 'load average: 9.80, 12.58, 13.56 '
, 'noiseload average: 9.80, 0.58, 13.56noisestatic  '
, ' load average: 9.80, -3.58, 13.56 noisestatic  '
)
;


// A FUNCTION TO ISOLATE THE SECOND INTEGER VALUE
function second_integer($str, $signal='load average:', $dlm=',')
{
    // PAD THE STRING
    $str = ' ' . $str . ' ';

    // DECLOP THE STRING AND RETURN THE CORRECT INFORMATION
    $arr = explode($signal, $str);
    $arr = explode($dlm, $arr[1]);
    return intval($arr[1]);
}

// TEST THE FUNCTION
foreach ($things as $thing)
{
    echo second_integer($thing);
    echo PHP_EOL;
}

Open in new window

0
 
willsherwoodAuthor Commented:
sorry my problems statement was not clearer,  the second solution works fine.
thanks all!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now