Separating strings in PHP and getting the last part

I need to break the string in PHP into "-" and get the last part.

So from this:

ABC-123-hug-789

I expect to receive

"789"

This is the code I came up with:

substr(strrchr($urlId, '-'), 1) 

which works fine except:

If my input line does not contain a "-", I should get the whole line, for example:

123

I need to come back

123

and it should be as fast as possible. Any help is appreciated!

+31
source share
11 answers

split($pattern,$string) splits strings in a given pattern or regular expression (not recommended since 5.3.0)

preg_split($pattern,$string) splits strings in a given regular expression pattern

split explode($pattern,$string) split strings within a given pattern

end($arr) get the last element of the array

So:

end(split('-',$str))

end(preg_split(' / -/',$str))

$strArray = explode('-',$str)
$lastElement = end($strArray)

Will return the last element - split line.


And there is a hardcore way to do this:

 $str = '1-2-3-4-5'; echo substr($str, strrpos($str, '-') + 1); // | '--- get the last position of '-' and add 1(if don't substr will get '-' too) // '----- get the last piece of string after the last occurrence of '-' 
+96
source
 $string = 'abc-123-xyz-789'; $exploded = explode('-', $string); echo end($exploded); 

EDIT :: Finally managed to remove the E_STRICT problem

+18
source

Just check if the separator character exists or not or not:

 if (strpos($potentiallyDelimitedString, '-') !== FALSE) { found delimiter, so split } 
+7
source

This code will do it

 <?php $string = 'abc-123-xyz-789'; $output = explode("-",$string); echo $output[count($output)-1]; ?> 
+2
source

As others have already mentioned, if you do not assign the result of the variable explode() variable, you will receive a message:

E_STRICT: Strict standards: only variables should be passed by reference

The right way:

 $words = explode('-', 'hello-world-123'); $id = array_pop($words); // 123 $slug = implode('-', $words); // hello-world 
+2
source

Since explode() returns an array, you can add square brackets directly to the end of this function if you know the position of the last element of the array.

 $email = ' name@example.com '; $provider = explode('@', $email)[1]; echo $provider; // example.com 

Or another way is list() :

 $email = ' name@example.com '; list($prefix, $provider) = explode('@', $email); echo $provider; // example.com 

If you do not know the position:

 $path = 'one/two/three/four'; $dirs = explode('/', $path); $last_dir = $dirs[count($dirs) - 1]; echo $last_dir; // four 
0
source

Like this post :

 end((explode('-', $string))); 

which will not trigger an E_STRICT warning in PHP 5 ( PHP magic ). Although the warning will be released in PHP 7 , so adding the @ in front of it can be used as a workaround.

0
source

To satisfy the requirement that β€œit should be as fast as possible”, I conducted a comparative analysis of some possible solutions. Each solution had to satisfy this set of tests.

 $cases = [ 'aaa-zzz' => 'zzz', 'zzz' => 'zzz', '-zzz' => 'zzz', 'aaa-' => '', '' => '', 'aaa-bbb-ccc-ddd-eee-fff-zzz' => 'zzz', ]; 

Here are the solutions:

 function test_substr($str, $delimiter = '-') { $idx = strrpos($str, $delimiter); return $idx === false ? $str : substr($str, $idx + 1); } function test_end_index($str, $delimiter = '-') { $arr = explode($delimiter, $str); return $arr[count($arr) - 1]; } function test_end_explode($str, $delimiter = '-') { $arr = explode($delimiter, $str); return end($arr); } function test_end_preg_split($str, $pattern = '/-/') { $arr = preg_split($pattern, $str); return end($arr); } 

Here are the results after each decision has been executed 1,000,000 times in test cases:

 test_substr : 1.706 sec test_end_index : 2.131 sec +0.425 sec +25% test_end_explode : 2.199 sec +0.493 sec +29% test_end_preg_split : 2.775 sec +1.069 sec +63% 

Turns out the fastest of them was using substr with strpos . Note that in this solution, we must check strpos for false so that we can return the full string (satisfying the zzz case).

0
source

You can use array_pop in conjunction with explode

the code:

 $string = 'abc-123-xyz-789'; $output = array_pop(explode("-",$string)); echo $output; 

DEMO : Click here

-1
source

You can do it like this:

 $str = "abc-123-xyz-789"; $arr = explode('-', $str); $last = array_pop( $arr ); echo $last; //echoes 789 
-1
source

You can do it as follows:

 $str = "abc-123-xyz-789"; $last = array_pop( explode('-', $str) ); echo $last; //echoes 789 
-2
source

Source: https://habr.com/ru/post/1402173/


All Articles