php - How to extract num part of a string -


i have string "my string 2" , num value "2". try see below result egal 0.

$alphasurv1="my string 2"; $numsurv1=intval($alphasurv1); echo $numsurv1; 

can me solve that?

thanks

in order capture last group of digits in string, can use regular expression:

$str = "123 test 29"; preg_match("/[0-9]+$/", $str, $matches); var_dump($matches[0]); # 29 

this work correctly following cases, including lack of whitespace, leading digits , multiple digits @ end of string:

"1"        => 1 "test 2"   => 2 "test 34"  => 34 "5 test 6" => 6 "7test8"   => 8 

note if want last character in string , treat number can use following:

$str = "my string 2";  $digit = intval(substr($str, -1)); 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -