If the nomenclature of substr is:
string substr ( string $string , int $start [, int $length ] )
How do I tell it that it has to take me the start string until it finds a "." of the extension?
That is: if we have "string.php", extract only "string".
You can do it by combining
substr
withstrpos
, or by exploding with.
Option 1 is closer to what you're trying to do, but if you pass it a character that isn't in the string, the result will be an empty string.
With the always option
explode
it will give you something back.Now, if you want it to return the string from the beginning to the last occurrence of a character, there you have to start putting checks, for example using
strpos
to verify that the character exists, or exploiting and verifying the length of the resulting array. Let's say for example your string isarchivo.con.datos.php
and you want justarchivo.con.datos
substr()
it only allows you to start from the indicated position and continue the number of characters that you indicate.Use explode()
With
substr()
you can't tell the character you want to end on, that's whatexplode()
.You can use strtok() , which splits a string into smaller strings and returns it to you as a string .