I have a logic problem, I'm trying to extract the numbers from a url for example:
this prints
888888
When in the url they send a normal number, everything works correctly.
The problem occurs when they send me a number that starts with ZERO and has more numbers, for example:
http://localhost/apiescuela/tareas/088888
Doesn't print anything...
I have the following code
if(isset($_GET['url'])){
$var = $_GET['url'];
$numero = intval(preg_replace('/[^0-9]/','',$var),10);
print_r($numero);
}
PHP is not as strict a language as others. For example, with the handling of numbers, PHP is somewhat free and you can do things that other languages do not allow you.
In your case, the problem is that when you convert to an integer with
intval
PHP, it removes the leading zeros, since they are useless in an integer.If you treat the value as a string you won't have any problems:
Note that you can even do operations with that value.
Let's try:
Departure:
NOTE:
Perhaps in this case it is not necessary to use regular expressions for something as simple as getting the last part of the URL. You could resort to lighter methods, like a simple
explode
:Departure: