-2

I have long string which can contain ids like desk_123 desk_456 desk_678 means static text desk_ and any numbers. I want to extract this type of id from a text please help

2 Answers 2

0

This should help you

$str = 'desk_123';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
3
  • well, if he wants to extract the desk_ also the regexp should be /(desk_\d+)/
    – mTorres
    Aug 17, 2014 at 17:06
  • 1
    @mTorres yes but he wanted to only extract the ID, otherwise he would of just called the string in the first place.
    – jagmitg
    Aug 17, 2014 at 17:20
  • Well for what I understand of the question, the srring is not desk_\d+ but a ling string with the pattern in it... otherwise you are absolutely right :-)
    – mTorres
    Aug 18, 2014 at 7:28
0
$deskNumbers[] = array_map(
    function($value) {
        sscanf($value, 'desk_%d');
    },
    explode(' ', $myVeryLongString)
);

Not the answer you're looking for? Browse other questions tagged or ask your own question.