Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strpos function in reverse in php

Tags:

string

php

strpos

People also ask

What is the use of strpos () function in PHP?

strpos in PHP is a built-in function. Its use is to find the first occurrence of a substring in a string or a string inside another string. The function returns an integer value which is the index of the first occurrence of the string.

What is strpos ()? Give example?

The strpos() function finds the position of the first occurrence of a string inside another string. Note: The strpos() function is case-sensitive. Note: This function is binary-safe.

What is Strrpos PHP?

The strrpos() function finds the position of the last occurrence of a string inside another string. Note: The strrpos() function is case-sensitive. Related functions: strpos() - Finds the position of the first occurrence of a string inside another string (case-sensitive)

How do I search for a specific word in a string in PHP?

Answer: Use the PHP strpos() Function You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .


int strrpos ( string $haystack , string $needle [, int $offset = 0 ] )

Find the numeric position of the last occurrence of needle in the haystack string.

http://php.net/manual/en/function.strrpos.php


What you need is strrpos to find the position of the last occurrence of a substring in a string

$string = "Kelley";
$strposition = strrpos($string, 'e');
var_dump($strposition);

Try this:

strrpos()

Hope that helps.


strripos and strrpos add $needle length to result, eg.:

<?php
$haystack = '/test/index.php';
$needle   = 'index.php';

echo strrpos($haystack, $needle);//output: 6

An alternative is use strrev for retrieve position from the end, eg:

<?php
$haystack = 'Kelley';
$needle   = 'e';

echo strpos(strrev($haystack), strrev($needle));//Output: 1

Simply as can be: strrpos()

This will return the first occurence of the character from the right.