Php 如何让strpos()区分';1';和';10';一串?

Php 如何让strpos()区分';1';和';10';一串?,php,Php,我有PHP代码,可以根据URL中的查询字符串更改页面标题。但是,这个查询字符串是由递增的数字组成的,当它包含数字10(或11、12等)时,它将使用数字1的变量。STRPO有没有办法看看是否有区别 片段: <?php $fullurl = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $title = ""; $meta = ""; if (strpos($fullurl,'planes-trains-automo

我有PHP代码,可以根据URL中的查询字符串更改页面标题。但是,这个查询字符串是由递增的数字组成的,当它包含数字10(或11、12等)时,它将使用数字1的变量。STRPO有没有办法看看是否有区别

片段:

<?php
$fullurl = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$title = "";
$meta = "";

if (strpos($fullurl,'planes-trains-automobiles') !== false) {
    if (strpos($fullurl,'mag=1') !== false) {
        $title = "Title 1";
        $meta = "Meta 1";
    }
    else if (strpos($fullurl,'mag=2') !== false) {
        $title = "Title 2";
        $meta = "Meta 2";

    }
...
else if (strpos($fullurl,'mag=10') !== false) {
        $title = "Title 10";
        $meta = "Meta 10";

    }

虽然问题本身不是答案,但只需重新排序IF/ELSE子句,以便检查10、11。。。在检查1之前:

<?php
if (strpos($fullurl,'planes-trains-automobiles') !== false) {
    if (strpos($fullurl,'mag=10') !== false) {
        $title = "Title 10";
        $meta = "Meta 10";
    }
    else if (strpos($fullurl,'mag=11') !== false) {
        $title = "Title 11";
        $meta = "Meta 11";

    }
...
    else if (strpos($fullurl,'mag=1') !== false) {
        $title = "Title 1";
        $meta = "Meta 1";   
    }


这样,如果从未到达mag=1,如果mag=10(11、12等,如果放在1之前)已经被选中。

不要通过匹配完整URL的字符串工作,使用解析的值!对于当前请求,您只需使用
$\u GET['mag']

if (isset($_GET['mag']) && ctype_digit($_GET['mag'])) {
    $title = 'Title ' . $_GET['mag'];
    $meta  = 'Meta ' . $_GET['mag'];
}
或者可能:

$titles = array(
    1 => 'Title 1',
    2 => 'Title 2',
    ...
);

if (isset($titles[$_GET['mag']])) {
    $title = $titles[$_GET['mag']];
}
或:


您可以使用$u GET['mag']。为了安全起见,您可以通过以下两种方法检查它是否为整数

$is_valid = is_numeric($_GET['mag']) && is_int(1*$_GET['mag']);

所以你会这样做:

if ($is_valid):
     die('wow so insecure, maybe');
else:
     //do whatever you want with $_GET['mag']

为什么不使用
preg\u match
提取数字

preg_match('/\d+/', $url, $m);
$title = "Title ".$m[0];
将匹配

$url= 'http://myurl-here.com?mag=10';
$url= 'http://myurl-here.com?mag=1';

etc

还有另一个url解析器吗?发现规律表达为什么否决这个答案?我(和用户1853181)回答了这个问题:“STRPO有没有办法看看是否有区别?”谢谢。简单有效。
if ($is_valid):
     die('wow so insecure, maybe');
else:
     //do whatever you want with $_GET['mag']
preg_match('/\d+/', $url, $m);
$title = "Title ".$m[0];
$url= 'http://myurl-here.com?mag=10';
$url= 'http://myurl-here.com?mag=1';