我有一个博客,我想在MAIN页面上显示ADS或特殊文本,但不能在URI /?paged=[number]
的任何其他页面上显示。我已经尝试过标准正则表达式/^[0-9].$/
,但它无法匹配。
我不确定我做错了什么或怎么做这个
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";
}
这就是我目前的做法,但我不想做成千上万的......
答案 0 :(得分:7)
你能否只检查paged
数组中是否存在GET
?
if(!isset($_GET['paged'])) {
// You know this is the main page.
}
答案 1 :(得分:0)
试试这个:
if (preg_match('#paged=\d+#', $_SERVER[REQUEST_URI]) {
echo "display special text, banners, and other stuff";
}
答案 2 :(得分:0)
正则表达式:/^[0-9].$/
对于“3k”字符串是正确的。分析这种模式
/page=(\d+)/
/page=([1-5])/
/^\/\?page=([1-5])$/
/page=(?<page>[1-5])/
答案 3 :(得分:0)
为什么不在GET参数中使用正则表达式?
<?php
$regexp = "/^(\d+)+$";
if (preg_match($regexp, $_GET['paged'])) {
#...your code
} else {
#...your code
}
或者(如果你想使用整个字符串)试试这个:
<?php
$regexp = "/^(\/\?paged)+=(\d+)+$/";