Count characters in a string from a search done in the string itself

-1

Good afternoon everyone! I would like your help to find out how I do the character count in a string by passing a reference in the string itself, eg: I have the string 19:30 and I wanted it after the : to check if the number of characters is greater than 2, if it was, it would do one thing, if it would not do another. I know how to do the part of the conditions, I'm just running into this doubt, how to count characters after a reference. Thank you!

    
asked by anonymous 20.04.2016 / 18:07

1 answer

1

You can use the strpos() function of php, it finds the position of the first occurrence of a substring in the string. If no occurrence is found, this function returns false .

The solution for you is this:

$string =  '19:30';

$pos    =  strpos( $string, ':' );

if( !is_bool( $pos ) ) {
    $pos += 1;
    echo strlen( substr( $string, $pos) );
}

I hope it helps.

    
20.04.2016 / 18:27