Sanity check on using intval

Hi all,

I’m using the following code to extract the letters I need from a mixed string.

The string will always be in the format of ‘letters’ then ‘numbers’ but the amount of letters may vary.

In the code below I’ve used the intval function to get first instance of an integer and I would just like to confirm with you clever people whether this method would be safe enough to use??

$string = "AAAAAA1234567890";
$suffix = "";

for($char=0; $char<strlen($string); $char++)
		$suffix .= $string[$char];
	} else

echo "Your suffix is " .$suffix;

Any help would be great


I’d probably use something similar to this:

function getNumericSuffix($string){
  $elements = array();
  preg_match('~[0-9]+$~', $string, $elements);
  return array_shift($elements);

echo getNumericSuffix('AAAAAA1234567890');

Of course, there’s loads of ways to skin a cat.

Thank you for your prompt reply. Yep you’re right, I was mainly concerned with intval and for the purpose I was using it for, but your way looks a lot leaner!!

Ah, your method seems to return the numbers rather than the letters

Oh, you need the letters!

$elements = array();
preg_match('~^[a-z]+~i', 'AAAAAA1234567890', $elements);
$letters = array_shift($elements);

Thank you!


sscanf("AAAAAA1234567890", "%[A-Z]", $letters);

Off Topic:

I couldn’t resist, sorry Anthony. (:

Ha, no worries. Remind me to “thank” you at PHPNW11. :wink: