tags:

views:

69

answers:

3
$vari = "testing 245";
$numb = 0..9;

$numb_pos =  strpos($vari,$numb);
echo substr($vari,0,$numb_pos);

The $numb is numbers from 0 to 9 Where am I wrong here, all I need to echo is testing

A: 

Your code won't work as-is, as it'll fail if the number if the first character in the string. (You need to check $numb_pos !== false prior to the substr.)

Irrespective, if you just want to check for the existance of a number in a string, something like the following would probably be more efficient.

$digitMatched = preg_match('/\\d/im', $vari);
middaparka
A: 

Use a regular expression to strip numeric characters from your string.

or, use a regular expression to find the first instance of one either way...

Stephen Wrighton
how do I do that?
dave
robertbasic provided details.
Stephen Wrighton
+5  A: 

You want to cut out the numbers from a string?

$string = preg_replace('/(\d+)/', '', 'String with 1234 numbers');
robertbasic
could you explain this to me
dave
The (\d+) part gets all the numbers from the string provided and replaces them with the second parameter - in this case, with nothing.
robertbasic
does d+ do the trick to replace the numbers?
dave
This is the way to go. Your psudo code fails to work is because PHP doesn't take 0..9 style, you can do foreach(range(0..9) as $numb) {} instead
Jay Zeng