To find the kth index of a substring in a string in PHP, we can use the strpos
function. This function returns the first occurrence of a substring in a string, and taking the optional third parameter of the function, we can find the kth index of that occurrence.
Here's an example:
main.php317 chars12 lines
In this example, we first find the index of the first occurrence of the substring using the strpos
function. Then we loop from 1 to k-1 (since we already have the index of the first occurrence), and in each iteration, we find the next occurrence of the substring using strpos
with the third parameter set to the index of the previous occurrence plus 1. Finally, we have the kth index of the substring in the variable $index
.
gistlibby LogSnag