Find string character position and remove it

Asked

Viewed 81 times

0

I have a complicated problem to solve, there are 3 types of variables that receive data via post example:

$mes = "08";
$mes = "10";
$mes = "12";

In the case of 3 different types of string, I need 0 to be removed when it is the first occurrence in the string (for the initial character) I have tried in several ways until it explodes in the data input, but this affects all input data and would not be necessary as this validation should only occur from number 01 to 09 leaving only unit 1 to 9. How can I do this ?

  • str_replace('0', '',$mes); ?

1 answer

5

You do not need to use a function to remove zero from the first position. You can type using int.

$mes = "08"; // string(2) "08"
$mes = "10"; // string(2) "10"
$mes = "12"; // string(2) "12"

$mes = (int) "08"; // int(8)
$mes = (int) "10"; // int(10)
$mes = (int) "12"; // int(12)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.