Search code examples
phpregexstringcharacterprefix

PHP get file name prefix?


How can I get the prefix of the current page? i.e. if the current page is called adm_panel.php, I would just like to extract adm_. The prefixes for pages vary in length so doing a substr for the first n characters wouldn't work.

I can get the page name just fine:

<?php
    echo '<p>' . basename($_SERVER['PHP_SELF']) . '</p>';
?>

But am stumped about how to get the prefix. Any ideas? I was thinking maybe regex?

Thanks.


Solution

  • reset(explode('_',$filename));
    

    or

    substr($filename, 0, strpos($filename, '_'));