-3

I got this result from mysql query using php:

chris123

I want to separate chris from 123 because I am going to use the value of int for operation.

$char=string('chris123');
$int=int('chris123');

How am I able to do it in PHP? Not PDO. Not so familiar with it. Thanks.

Dalton Cézane
  • 3,672
  • 2
  • 35
  • 60
kristyan
  • 1
  • 2
  • 6
    Possible duplicate of [Split String into Text and Number](https://stackoverflow.com/questions/14348018/split-string-into-text-and-number) – No Name Aug 09 '18 at 18:27
  • Definitely a duplicate as @BojanSrbinoski stated. Follow that link and it'll do exactly as you are seeking. – dmotors Aug 09 '18 at 18:38

1 Answers1

1

If you know the non-number parts are always going to be lowercase characters, you can do this:

$str = 'chris123';
$num = trim($str, 'a..z');

If you also want it to be an actual integer, you can cast it:

$str = 'chris123';
$num = (int) trim($str, 'a..z');
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98