Given
$str = "asd/fgh/jkl/123
If we want to get string piece after last slash , we can use function strrchr()
right?
In php not function, to get string piece, before last slah, that is asd/fgh/jkl
?
I know this can make via regex or other way, I am asking about internal function?
Use the substring() method to get the substring before a specific character, e.g. const before = str. substring(0, str. indexOf('_')); . The substring method will return a new string containing the part of the string before the specified character.
substr in PHP is a built-in function used to extract a part of the given string. The function returns the substring specified by the start and length parameter. It is supported by PHP 4 and above.
The endsWith() function is used to test whether a string ends with the given string or not. This function is case insensitive and it returns boolean value.
The strpos() finds the offset of the underscore, then substr grabs everything from that index plus 1, onwards.
You can do this by:
explode
— Split a string by string (Documentation)
$pieces = explode("/", $str );
example
$str = "asd/fgh/jkl/123";
$pieces = explode("/", $str );
print_r($pieces);
$count= count($pieces);
echo $pieces[$count-1]; //or
echo end($pieces);
Codepad
You can use
$str = "asd/fgh/jkl/123";
echo substr($str, 0,strrpos($str, '/'));
Output
asd/fgh/jkl
$str = "asd/fgh/jkl/123";
$lastPiece = end(explode("/", $str));
echo $lastPiece;
output: 123;
explode() converts the string into an array using "/" as a separator (you can pick the separator)
end() returns the last item of the array
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With