1

I have 2 variable 1 my file name and 1 my file name 12 .I want to check wheter there have a digit at the end or not.

moto g
  • 13
  • 2

2 Answers2

1

Try this...

$test="1 my file name 12";
$te = preg_match_all("/.*?(\d+)$/", $test, $digit);

if($te>0) {
    echo $test." have following digit in end".$digit[count($digit)-1][0];
}

Result: 1 my file name 12 have following digit in end 12

Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50
0

See the simple regex below:

$var = 'my file name 2';

if (preg_match('~\d$~', $var)) {
    echo 'second filename ends with a digit';
}
pavel
  • 26,538
  • 10
  • 45
  • 61