find the last index of a character in a string in php

To find the last index of a character in a string in PHP, you can use the strrpos() function. This function returns the position of the last occurrence of a substring in a string. Here's an example:

main.php
$string = "Hello, world!";
$last_comma = strrpos($string, ",");
echo $last_comma; // Outputs: 5
96 chars
4 lines

In this example, we're searching for the last occurrence of the comma "," in the string "Hello, world!". The strrpos() function returns the index of the last comma, which is 5.

If the character is not found in the string, strrpos() returns false.

related categories

gistlibby LogSnag