10

I have many strings that follow the same convention:

this.is.a.sample
this.is.another.sample.of.it
this.too

What i want to do is isolate the last part. So i want "sample", or "it", or "too".

What is the most efficient way for this to happen. Obviously there are many ways to do this, but which way is best that uses the least resources (CPU and RAM).

alecwhardy
  • 2,698
  • 6
  • 27
  • 33

6 Answers6

31
$string = "this.is.another.sample.of.it";
$contents = explode('.', $string);

echo end($contents); // displays 'it'
Menztrual
  • 40,867
  • 12
  • 57
  • 70
  • Is this actually faster than using strrchr && strrpos? substr($string, strrchr($string, '.')+1) – Rein Baarsma Aug 11 '16 at 11:43
  • It seems to be the case: [What is more efficient?](https://stackoverflow.com/questions/18826797/php-listexplode-vs-substrstrpos-what-is-more-efficient). Tests were done with PHP5 though. –  Jan 23 '20 at 08:32
7

I realise this question is from 2012, but the answers here are all inefficient. There are string functions built into PHP to do this, rather than having to traverse the string and turn it into an array, and then pick the last index, which is a lot of work to do something quite simple.

The following code gets the last occurrence of a string within a string:

strrchr($string, '.'); // Last occurrence of '.' within a string

We can use this in conjunction with substr, which essentially chops a string up based on a position.

$string = 'this.is.a.sample';
$last_section = substr($string, (strrchr($string, '-') + 1));
echo $last_section; // 'sample'

Note the +1 on the strrchr result; this is because strrchr returns the index of the string within the string (starting at position 0), so the true 'position' is always 1 character on.

Mike
  • 8,767
  • 8
  • 49
  • 103
  • This is good advice, though there's a small issue in your actual code. strrchr returns the portion of the string after and including the given char, not a numeric index. So you would want to do $last_section = substr(strrchr($string, '.'), 1); to get everything after the char. – Mark Oct 03 '18 at 07:56
3

Just do:

$string = "this.is.another.sample.of.it";
$parts = explode('.', $string);
$last = array_pop(parts);
Max Cuttins
  • 614
  • 2
  • 6
  • 20
3

http://us3.php.net/strpos

$haystack = "this.is.another.sample.of.it"; 
$needle = "sample"; 
$string = substr( $haystack, strpos( $haystack, $needle ), strlen( $needle ) ); 
phpmeh
  • 1,752
  • 2
  • 22
  • 41
0
$new_string = explode(".", "this.is.sparta");
$last_part = $new_string[count($new_string)-1];

echo $last_part;    // prints "sparta".
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
0
$string = "this.is.another.sample.of.it";
$result = explode('.', $string); // using explode function

print_r($result); // whole Array

Will give you

result[0]=>this;
result[1]=>is;
result[2]=>another;
result[3]=>sample;
result[4]=>of;
result[5]=>it;

Display any one you want (ex. echo result[5];)

Ajay Patel
  • 5,298
  • 11
  • 55
  • 87