Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

strrpos

Finds the position of the last occurrence of a substring.

Syntax

strrpos(string $haystack, string $needle, int $offset = 0): int|false

Parameters

ParameterTypeDescription
$haystackstringThe string to search in.
$needlestringWhat to look for.
$offsetintOptional. 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
gz

Notes

For a file extension pathinfo($file, PATHINFO_EXTENSION) is more reliable.

See also

  • strpos — Finds the position of the first occurrence of a substring.
  • substr — Returns part of a string, counting in bytes.
  • pathinfo — Splits a path into parts: the folder, the name and the extension.

Related recipes

In the tutorial

  • Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.

Thanks for reading!

Donate to the site — we work hard for you.