2

I've got a blog, I want to display ADS or special text on the MAIN page but not on any other page with the URI /?paged=[number]. I've tried the standard regex /^[0-9].$/ but it fails to match.

I'm not sure what I've done wrong or how to go about doing this

if (substr_count($_SERVER[REQUEST_URI], "/") > 0 AND
substr_count($_SERVER[REQUEST_URI], "/?paged=1") == 0 AND
substr_count($_SERVER[REQUEST_URI], "/?paged=2") == 0 AND
substr_count($_SERVER[REQUEST_URI], "/?paged=3") == 0 AND
substr_count($_SERVER[REQUEST_URI], "/?paged=4") == 0 AND
substr_count($_SERVER[REQUEST_URI], "/?paged=5") == 0) {
echo "display special text, banners, and other stuff";
}

This is how I'm doing it currently, but I don't want to do thousands of these...

11
  • 7
    Why not just if(!isset($_GET['paged'])) ? Commented May 13, 2013 at 12:26
  • 1
    When I see code like this I'm reminded of why I don't miss thedailywtf.com anymore since I became more active on SO. Commented May 13, 2013 at 12:30
  • 1
    If you take a look at your logical comparison IT HAS NO SENSE. You are checking if your URI have "/?paged=1", "/?paged=2", "/?paged=3", "/?paged=4", "/?paged=5" at the same time and this will NEVER match, obviusly. You have to add OR instead of AND Commented May 13, 2013 at 12:30
  • 1
    IT HAVE NO SENSE.. Oh the irony. Commented May 13, 2013 at 12:31
  • btw, /^[0-9].$/ means, a digit, then any character. The dot is a wildcard itself. Perhaps you meant to use + ? Commented May 13, 2013 at 12:32

4 Answers 4

7

Can you not just check for the presence of paged in the GET array?

if(!isset($_GET['paged'])) {
    // You know this is the main page.
}
Sign up to request clarification or add additional context in comments.

2 Comments

Plus one. And even if you want to make sure it's a valid number - then make sure those valid numbers are stored somewhere so you can check it against a database. There's no way those should be hardcoded in.
@WolfmanJoe Agreed. It's often best to port values like that into some persistent structure, so your code is as flexible as possible.
0

Try this:

if (preg_match('#paged=\d+#', $_SERVER[REQUEST_URI]) {
    echo "display special text, banners, and other stuff";
}

Comments

0

Regex: /^[0-9].$/ would be correct for "3k" string. Analize this patterns

/page=(\d+)/
/page=([1-5])/
/^\/\?page=([1-5])$/
/page=(?<page>[1-5])/

Comments

0

Why not using the regexp in the GET parameter ?

<?php
$regexp = "/^(\d+)+$";
if (preg_match($regexp, $_GET['paged'])) {
    #...your code
} else {
    #...your code
}

Or (if you want to use the entire string) try this:

<?php
$regexp = "/^(\/\?paged)+=(\d+)+$/";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.