alex alex - 4 years ago 355
PHP Question

PHP: Find last charactor position in strpos

suppose there is string like


I'm trying to find the position of
but not the
word using
, I can achieve this by
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