How do I test a variable against each entry in an array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adders
Forum Newbie
Posts: 9
Joined: Thu Oct 26, 2006 2:37 am

How do I test a variable against each entry in an array?

Post by adders »

Hello All

Does anyone know how I can test a variable against each entry in an array.

I will have code loosely like this:

Code: Select all

<head>
$url = substr($_SERVER['REQUEST_URI'],1);;
$nodisplay = "vos_new/index.php?option=com_frontpage&Itemid=1";
</head>
<body>
   <?php
            if ($nodisplay == $url)
               {
               return;
               }
            else
               {
               mosLoadModules ( 'header', -2);
               } 
   ?>
</body>
I would like to be able to store multiple values in $nodisplay, then check $url against each in turn and if it matches any of them I want to exit the script without running mosLoadModules.

Alternatively creating a URL pattern might work so if someone knows how to do that and store it in a variable that might be useful also.

Thanks

Adders
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

Code: Select all

<head>
</head>
<body>
   <?php
           $url = substr($_SERVER['REQUEST_URI'],1);
           $nodisplay = Array("vos_new/index.php?option=com_frontpage&Itemid=1", "index.php?something=else", "...");

            if (!in_array($url, $nodisplay))
               mosLoadModules ( 'header', -2);
   ?>
</body>
adders
Forum Newbie
Posts: 9
Joined: Thu Oct 26, 2006 2:37 am

Post by adders »

Thanks Kaszu.

You've been a great help today.

Cheers

Adders
adders
Forum Newbie
Posts: 9
Joined: Thu Oct 26, 2006 2:37 am

Post by adders »

OK - I promise this is the last question today.

With regard to this query, what would I do if I want to match part of a URL (I would have several of these partial URLs stored in my array).

In this case anything with index.php?option=com_joomlaboard&Itemid=27&func=post in it (or at the start of it).

So that for example my code would run on this page:

index.php?option=com_joomlaboard&Itemid=27&func=view&catid=43&id=27180#27180

But not on this page:

index.php?option=com_joomlaboard&Itemid=27&func=post&do=edit&id=27180&catid=43

If you guys can help with this I promise never to darken your doors again (well not for the rest of the day anyway).

Cheers

Adders
adders
Forum Newbie
Posts: 9
Joined: Thu Oct 26, 2006 2:37 am

Post by adders »

Anyone got any ideas on the above?

Cheers

Adders
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

foreach and strncmp might do the trick.
adders
Forum Newbie
Posts: 9
Joined: Thu Oct 26, 2006 2:37 am

Post by adders »

Cheers mate I'll take a look.

Thanks

Adders
Post Reply