16

I have strings:

$one = 'foo bar 4 baz (5 qux quux)';
$two = 'bar baz 2 bar';
$three =  'qux bar 12 quux (3 foo)';
$four = 'foo baz 3 bar (13 quux foo)';

How can I find the numeric digits in these strings?

Maybe with function:

function numbers($string){

    // ???

    $first = ?;
    $second = ?;
}

For example:

function numbers($one){

    // ???

    $first = 4;
    $second = 5;
}

function numbers($two){

    // ???

    $first = 2;
    $second = NULL;
}

Best way for this maybe is regex, but how can I use this for my example? Maybe without regex?

4
  • You want to match only numerics? Not to convert one to 1 as well? Commented Jun 28, 2012 at 11:19
  • Your question is not clear, do you want to extract only numbers from a string? Commented Jun 28, 2012 at 11:19
  • Have a look at this... it will point you in direction... stackoverflow.com/questions/1077600/… Commented Jun 28, 2012 at 11:20
  • yes, i want to match only numerics. one, two etc are only examples words Commented Jun 28, 2012 at 11:24

3 Answers 3

42

You can use regular expressions for this. The \d escape sequence will match all digits in the subject string.

For example:

<?php

function get_numerics ($str) {
    preg_match_all('/\d+/', $str, $matches);
    return $matches[0];
}

$one = 'foo bar 4 baz (5 qux quux)';
$two = 'bar baz 2 bar';
$three = 'qux bar 12 quux (3 foo)';
$four = 'foo baz 3 bar (13 quux foo)';

print_r(get_numerics($one));
print_r(get_numerics($two));
print_r(get_numerics($three));
print_r(get_numerics($four));

https://3v4l.org/DiDBL

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

Comments

7

You can do:

$str = 'string that contains numbers';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);

Comments

5

Here's my attempt WITHOUT a regular expression

function getNumbers($str) {
    $result = array();

    // Check each character.
    for($i = 0, $len = strlen($str); $i < $len; $i++) {
        if(is_numeric($str[$i])) {
            $result[] = $str[$i];
        }
    }

    return $result;
}

$one = 'one two 4 three (5 four five)';
$two = 'one two 2 three';
$three =  'one two 12 three (3 four)';
$four = 'one two 3 three (13 four five)';

var_dump(getNumbers($one));
var_dump(getNumbers($two));
var_dump(getNumbers($three));
var_dump(getNumbers($four));

// Output:

array(2) {
  [0]=>
  string(1) "4"
  [1]=>
  string(1) "5"
}

array(1) {
  [0]=>
  string(1) "2"
}

array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
}

array(3) {
  [0]=>
  string(1) "3"
  [1]=>
  string(1) "1"
  [2]=>
  string(1) "3"
}

1 Comment

WITHOUT a regular expression - Admirable, but silly for this job. Regex will be much better/faster here - especially since you are calling strlen() on every iteration rather that caching in a variable the result once before the loop. Try this with a large string (or maybe even a small one) and you will find regex is much faster.

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.