strrpos
Finds the position of the last occurrence of a substring.
Syntax
strrpos(string $haystack, string $needle, int $offset = 0): int|falseParameters
| Parameter | Type | Description |
|---|---|---|
| $haystack | string | The string to search in. |
| $needle | string | What to look for. |
| $offset | int | Optional. Where to start. |
Return value
int|falseThe position of the last occurrence, or `false`.
Examples
Everything after the last dot:
<?php
$file = 'archive.tar.gz';
echo strrpos($file, '.'), "\n";
echo substr($file, strrpos($file, '.') + 1);Output:
11
gzNotes
For a file extension pathinfo($file, PATHINFO_EXTENSION) is more reliable.
See also
Related recipes
In the tutorial
- Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.