0

I am successfully extracting integers from string.

How to do it but including strings: "k ", "m " and "b "?

This code takes given website code and extracts numbers, I need numbers and "k ", "m " and "b "

$objectId = 15259;
$lines = file('http://testwebsite.com?obj=' . $objectId);
foreach ($lines as $line_num => $line) {
    $lineCrawl = htmlspecialchars($line);
    $stringSum = "$stringSum$lineCrawl";
}

function get_string_between($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}

$parsed = get_string_between($stringSum, "Current guide price", "Today");

$guideprice = ereg_replace("[^0-9]", "", $parsed); 

echo $guideprice; 
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Szymon Toda
  • 4,454
  • 11
  • 43
  • 62
  • So that future SO readers can fully understand your task, please add context to this question by offering one or more input strings and your expected output. – mickmackusa Oct 15 '17 at 00:59

2 Answers2

1

Change 0-9 to 0-9kmb should work

Ethan H
  • 717
  • 1
  • 13
  • 27
  • Without the coma, Just [0-9kmb]. Or else, a coma would be removed too. – mpratt May 01 '12 at 22:31
  • While this answer may have solved the issue, it does very little inform future SO readers. Please improve your answer by offering some supporting information, doc references, and/or explanation. I know it is a simple answer, but this question may be linked to a future duplicate page and those readers may want to understand _why_ this answer is right to use. – mickmackusa Oct 15 '17 at 01:01
0

Changing the code around to make better use of regex:

<? //PHP 5.4+
$matches = [];
\preg_match(
    <<<'REGEX'
`(?x)
    Current \s guide \s price
    \D* (?<price> \d+ [kmb] \s )
    Today
`u
REGEX
,   \file_get_contents('http://testwebsite.com?obj=' . 15259),
    $matches
);
echo $matches['price'];
?>
Cory Carson
  • 276
  • 1
  • 6
  • Is there any difference? Never heard of REGEX – Szymon Toda May 02 '12 at 11:10
  • While the notion of combining `get_string_between()` with `ereg_replace()` and using `preg_match()` is a wise choice (best practice), I believe your pattern is not going to be successful. If the OP updates the question to show some sample input, then a better pattern should be posted. – mickmackusa Oct 15 '17 at 01:13