geekism
geekism

Reputation: 53

PHP regex numbers in url

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...

Upvotes: 2

Views: 155

Answers (4)

christopher
christopher

Reputation: 27346

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.
}

Upvotes: 7

Cristian
Cristian

Reputation: 327

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+)+$/";

Upvotes: 0

bato3
bato3

Reputation: 2815

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

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

Upvotes: 0

Stephan
Stephan

Reputation: 43013

Try this:

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

Upvotes: 0

Related Questions