How to remove numbers from the end of a string in php

I want to remove numbers from the end of a string in PHP if numbers exist at the end of the string. I'm not sure how many digits there can be at the end.

any help would be appreciated.

thank

Regards,

+3


source to share


2 answers


Use preg_replace



$str = preg_replace('/\d+$/', '', $str);

      

-2


source


Have a look at using preg_replace.

$someString = "1111 this is my string with numbers at the start, in the middle 5555, and at the end 11134";
$newString = preg_replace("/\d+$/","",$someString);
echo $newString;

      



This will show you:

1111 this is my string with numbers at the start, in the middle 5555, and at the end 

      

+18


source







All Articles