2012-06-28 12 views
8

ho stringhe:Ottenere numeri da stringa con PHP

$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)'; 

Come posso trovare le cifre numeriche in queste corde?

Forse con funzione di:

function numbers($string){ 

    // ??? 

    $first = ?; 
    $second = ?; 
} 

Ad esempio:

function numbers($one){ 

    // ??? 

    $first = 4; 
    $second = 5; 
} 

function numbers($two){ 

    // ??? 

    $first = 2; 
    $second = NULL; 
} 

Il modo migliore per questo forse è regex, ma come posso usare questo per il mio esempio? Forse senza regex?

+0

Vuoi abbinare solo numeri? Non convertire anche 'one' in' 1'? – DaveRandom

+0

La tua domanda non è chiara, vuoi estrarre solo numeri da una stringa? – bodi0

+0

Date un'occhiata a questo ... si punta nella direzione ... http://stackoverflow.com/questions/1077600/converting-words-to-numbers-in-php – Brian

risposta

22

È possibile utilizzare regular expressions per questo. Lo \descape sequence corrisponderà a tutte le cifre nella stringa dell'oggetto.

Ad esempio:

<?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

6

si può fare:

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

Ecco il mio tentativo SENZA un'espressione regolare

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

"SENZA un'espressione regolare" - Ammirabile, ma sciocco per questo lavoro. Regex sarà molto meglio/più veloce qui - specialmente visto che stai chiamando 'strlen()' su ogni iterazione piuttosto che mettere in cache in una variabile il risultato una volta prima del ciclo. Prova questo con una stringa grande (o forse anche piccola) e troverai che la regex è molto più veloce. – DaveRandom

+0

Sono d'accordo, questo è sciocco, ma è quello che l'OP chiedeva :) – Greg

+0

che è super, uomo thanx – Mevia