سؤال

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.

هل كانت مفيدة؟

المحلول 2

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

or

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

نصائح أخرى

You can use the following regex:

^[^_]+

This will match everything from the start to the first _ (not including the _)

Eg: adm

Demo

To get the _ as well, change to regex to:

^[^_]+_

Try this:

$filename = basename($_SERVER['PHP_SELF']);
echo '<p>' . substr($filename, 0, strpos($filename, '_') + 1) . '</p>';
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top