Search code examples
phpsubstr

How to get 2nd variable of the string separated by dots?


I have categories ({PHP.c}) that have their codes - for example guide.cu OR guide.us OR story.cu etc and

With this function I can get the root category

function get_root_cat($code)
{
        return mb_substr($structure['page'][$code]['path'], 0, mb_strpos($structure['page'][$code]['path'], '.'));
}

So with {PHP.c|get_root_cat($this)} I can get 'guide' or 'story' etc.

But now structure gets more complicated and {PHP.c} looks like guide.cu.one, guide.cu.two or story.us.three

I need to find this: guide.cu.one or story.us.three


Solution

  • Does this solve your problem?

    return explode(".", $structure['page'][$code]['path'])[1];
    

    More info here: https://php.net/explode