alex alex - 4 years ago 355
PHP Question

PHP: Find last charactor position in strpos

suppose there is string like

localhost/images/pic-1.jpg
.

I'm trying to find the position of
s
but not the
/
in
/images
word using
strpos()
, I can achieve this by
strpos('localhost/images/pic-1.jpg','/images')+7
but it is not pleasant.

Answer Source

If I understand the question correctly, you're trying to find the index of the end of a substring within a larger string as opposed to its beginning, which strpos would return.

Your approach seems to be right, although it could be generalized a tad:

function strendpos ($haystack, $needle) {
    $pos = strpos($haystack, $needle);
    if ($pos) {
        $pos += strlen($needle);
    }
    return $pos;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download