简体   繁体   中英

PHP if statement help - syntax if URL is in specific directory?

I know how to edit the syntax of my PHP if statements when it relates to a specific page on my website. For example the following code works fine below:

<?php
if ($this->here == '/education') {?>
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php } ?>

However, I have a directory of pages that have a url like so:

site.com/c/sdflkjasldkjf  

For all the pages that are in the /c/ directory, I want to add an if statement like the one above so that if any page has a URL in the /c/ directory it will result in meta name="viewport" content="width=device-width, initial-scale=1

I tried the following but it doesn't appear to work:

if ($this->here == '/c/') {?>
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php } ?>

Is there something like a "contains" operator I can use instead of == ?

Thanks! Sorry I'm such a Noob - I'm trying to learn! :)

In this case: strpos() is the function you're looking for.

$test = strpos($this->here, '/c/');
if ($test !== false) {
  // Good to go
}

Check out the documentation here:

Php Manual

Maybe something like this:

<?php       $URlstr= "www.google.com/education";
        if (substr_compare(URlstr, "/education",0)) :?>
           <meta name="viewport" content="width=device-width, initial-scale=1">
        <?php endif; ?>

you can use built in string function of php engine, in this case we used " substr_compare " to view code running: http://codepad.org/0MuJcxXC

Couldn't get strpos to work. Ended up getting the following to solve the problem:

<?php if (mb_substr($this->here, 0, 3 ) === "/c/") {?>
       <meta name="viewport" content="width=device-width, initial-scale=1">
        <?php } ?>            

    <?php

Thanks for the help though!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM