How to find a specific string from a namespace using Laravel

I have a problem finding a specific string from a namespace App\Models\Wordpress\Wordpress. I want to get the last line WordPress, is there any way to find this?

+4
source share
2 answers

If you have a dynamic namespace, you can use the magic constant : __NAMESPACE__

$arr = explode("\\", __NAMESPACE__);
$last = end($arr);
+3
source

You can use explodeto split the string into a specific character ("\" in this case), and then get the final record.

$string = "App\Models\Wordpress\Wordpress";
$remove="\\";
$pieces = explode($remove, $string);
$last_word = array_pop($pieces);
echo $last_word;
+1
source

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


All Articles