J. Doe J. Doe - 3 months ago 7
PHP Question

PHP Execute if URL has prefix

I have multiple pages with the

magic
prefix, the URL is like so for the main page:


dashboard.php?page=magic


and for the other subpages like so:


dashboard.php?page=magic_dothisandthat


but that way I have to add them all to my stylesheet execution script;

if(isset($_GET['page']) && $_GET['page'] == 'magic') {
'<link rel="stylesheet" href="example">';
}


Is there a way to execute the stylesheet HTML on all pages which have the
magic_
prefix?

Example what I mean:

if($_GET['page'] == 'magic_' *) {

Answer

You can check if the first 5 characters of $page equal magic:

if(isset($_GET['page']) && substr($_GET['page'],0,5) == 'magic') {
     '<link rel="stylesheet" href="example">';
}

http://php.net/manual/en/function.substr.php

Comments