-2

Possible Duplicate:
PHP code to remove everything but numbers

i have a loop that returns some content and i want to filter so that i get back only numbers:

foreach ($number as $k => $content) {
    echo "<td>" . $content . "</td>";
}

this will return:

Sender Number,,
1818233483,, - aaa
1562595441,, - aaa
1870750493,, - aaa
1832677004,, - aaa
1832466803,, - aaa

and i want to do some king of regexp or something so i will filter only the numbers:

1818233483
1562595441
1870750493
1832677004
1832466803

also i'm not sure what characters might be there so i need to check for everything but numbers.

any ideas? thanks

edit: i did this: $test = preg_replace("/[^0-9]/", "", $content);

but i get:

Sender Number,,
1818233483
1562595441
1870750493
..
Community
  • 1
  • 1
Patrioticcow
  • 26,422
  • 75
  • 217
  • 337
  • explode on the comma, or if the length is fixed substr(), regex would be over kill –  Dec 20 '11 at 02:03
  • Does this answer your question? http://stackoverflow.com/questions/6604455/php-code-to-remove-everything-but-numbers – VikingBadger Dec 20 '11 at 02:05

2 Answers2

3

Use this:

foreach ($number as $k => $content) {
    echo "<td>" . preg_replace( '/[^0-9]/', '', $content) . "</td>";
}

Edit: try this to remove the Sender Number,,

foreach($number as $k => $content) {
    if(is_numeric(substr($content, 0, 4)) {
        echo "<td>" . preg_replace( '/[^0-9]/', '', $content) . "</td>";
    }
}
Léon Rodenburg
  • 4,894
  • 1
  • 18
  • 18
2

Try replacing every character that's not a digit with nothing, using preg_replace

foreach ($number as $k => $content) {
    echo "<td>" . preg_replace( '/[^0-9]/', '', $content) . "</td>";
}

A more efficient approach might be to do string processing on the comma, like so:

foreach ($number as $k => $content) {
    echo "<td>" . substr($content, 0, strpos( $content, ',')) . "</td>";
}
nickb
  • 59,313
  • 13
  • 108
  • 143