1

Consider a string as below .

$string="Lorem ipsum $ 1000 ,ipsum $2000 sopr $250 gerb $ 150 dfkuer fsdf erwer 1020 $ gsdfasdtwe qw $ 5000 efk kdfgksgdf 2000 $ sdhfgsd fsdf 620 $ sdfjg jsdf3000$";

I have to find out how many numbers are there within this string. But the number is equal to 1000 and above 1000 which proceed and followed by $ symbol .

Example : $1000 (or) $ 1000 (or) 1000$ (or) 1000 $ and above 1000 only .

3 Answers 3

1

Using preg_match_all() and a foreach loop:

$string="Lorem ipsum $ 1000 ,ipsum $2000 sopr $250 gerb $ 150 dfkuer fsdf erwer 1020 $ gsdfasdtwe qw $ 50000 efk kdfgksgdf 2000 $ sdhfgsd fsdf 620 $ sdfjg jsdf3000$";

preg_match_all('/(\$\s?)(?P<before>\d{4,})|(?P<after>\d{4,})(\s?\$)/', $string, $m);

$tmp = array_filter($m["before"]) + array_filter($m["after"]);
$number = array();
foreach($tmp as $n){
    if($n >= 1000){
        if(isset($number[$n])){
            $number[$n]++;
        }else{
            $number[$n] = 1;
        }
    }
}

print_r($number);
// Key => number, value => n occurences

I've used \d{4,} to match 4 digit numbers which are 1000 or higher, but say for example there is a number like 0500, this will also be matched. So I used a foreach loop to filter the numbers.

Sign up to request clarification or add additional context in comments.

9 Comments

I guess this will match a number even if it is not followed or preceded by $
Try a string like this : $string ="Lorem ipsum $ 1000 ,ipsum $2000 sopr $250 gerb $ 150 dfkuer fsdf 565656 erwer 1020 $ gsdfasdtwe qw $ 5000 efk kdfgksgdf 2000 $ sdhfgsd fsdf 620 $ sdfjg jsdf3000$"; your code will output 565656 also which is not supposed to
@PrasanthBendra why not o.O ? The OP said >= 1000 !
Should be followed or preceded by $
@PrasanthBendra Ah yes ofcourse, you already mentioned that in the first comment. I'm working on a quick solution, thank you anyways :)
|
0

Try this :

$string  ="Lorem ipsum $ 1000 ,ipsum $2000 sopr $250 gerb $ 150 dfkuer fsdf erwer 1020 $ gsdfasdtwe qw $ 5000 efk kdfgksgdf 2000 $ sdhfgsd fsdf 620 $ sdfjg jsdf3000$";

preg_match_all('/\$\s?(?P<pr>\d{4,})|(?P<fl>\d{4,})\s?\$/',$string,$match);
$res     = array_merge(array_filter($match['pr']),array_filter($match['fl']));

echo "<pre>";
print_r($res);

Output :

Array
(
    [0] => 1000
    [1] => 2000
    [2] => 5000
    [3] => 1020
    [4] => 2000
    [5] => 3000
)

2 Comments

Edited the answer please check it now
Thanks,Here your code its help me to solve $string ="Lorem ipsum DOLLAR 1000 ,ipsum $2000 sopr $250 gerb $ 150 dfkuer fsdf erwer 1020 $ gsdfasdtwe qw $ 5000 efk kdfgksgdf 2000 $ sdhfgsd fsdf 620 $ sdfjg jsdf3000$"; preg_match_all('/(?P<number>\DOLLAR\s?\d{4,}|\d{4,}\s?\$)/',$string,$match); echo "<pre>"; print_r($match['number']);
0
<?php
$string="Lorem ipsum $ 1000 ,ipsum $2000 sopr $250 gerb $ 150 dfkuer fsdf erwer 1020 $ gsdfasdtwe qw $ 5000 efk kdfgksgdf 2000 $ sdhfgsd fsdf 620 $ sdfjg jsdf3000$";
$pattern = "#([$][\s]*)?([1-9]\d{3})([\s]*[$])?#";
//(?<=$|$\s)
//(?=$|\s$)
preg_match_all($pattern, $string, $out);
print_r($out[2]);

Array
(
    [0] => 1000
    [1] => 2000
    [2] => 1020
    [3] => 5000
    [4] => 2000
    [5] => 3000
)

1 Comment

I guess this will match a number even if it is not followed or preceded by $

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.