PHP使用explode和if语句获取字符串

PHP使用explode和if语句获取字符串,php,redirect,if-statement,get,explode,Php,Redirect,If Statement,Get,Explode,我正在写我认为是一个简单的脚本,但我被卡住了 场景是我想从GET请求创建2个字符串 例如:domain.com/script.php?Client=A12345 在script.php中,需要获取“客户机”并创建2个变量。一个是$brand,需要从URL中获取A或B。另一个是$id,它需要从URL获取12345 现在,在它有了这两个变量$brand和$id之后,它需要有一个if语句来根据下面的品牌重定向 if ($brand=="A") { header('Location: http://a.

我正在写我认为是一个简单的脚本,但我被卡住了

场景是我想从GET请求创建2个字符串

例如:domain.com/script.php?Client=A12345

在script.php中,需要获取“客户机”并创建2个变量。一个是$brand,需要从URL中获取A或B。另一个是$id,它需要从URL获取12345

现在,在它有了这两个变量$brand和$id之后,它需要有一个if语句来根据下面的品牌重定向

if ($brand=="A") {
header('Location: http://a.com');
}
if ($brand=="B") {
header('Location: http://b.com');
在每个URL的末尾,我想要一个$id,但我不确定如何做到这一点

例如,我将访问domain.com/script?Client=A1234上的脚本,它需要将我重定向到a.com/12345

提前谢谢

你很容易做到

$value = $_GET['Client'];

$brand = substr($value, 0, 1);

$rest  = substr($value, 1, strlen($brand)-1);

现在您有了$brand字符串中的第一个字符,您可以执行if语句并按照您想要的方式重定向

你是说像这样吗?

注意:
只有当品牌长度仅为1个字符时,此选项才有效。如果不是这样,请给出更好的例子

<?php

$client = $_GET['Client'];
$brand = strtolower(substr($client, 0, 1));
$id = substr($client, 1);

if ($brand == 'a')
{
    header("Location: http://a.com/$id");
}
elseif ($brand == 'b')
{
    header("Location: http://b.com/$id");
}
?>

尝试使用:

preg_match("/([A-Z])(\d*)/",$_GET['Client'],$matches);
$matches[1]
将包含字母,
$matches[2]
将包含您的id

然后您可以使用:

if ($matches[1]=="A")
{
    header('Location: http://a.com/{$matches[2]}');
}
if ($matches[1]=="B")
{
    header('Location: http://b.com/{$matches[2]}');
}

我建议你也可以试试

$requested = $_GET["Client"];
$domain = trim(preg_replace('/[^a-zA-Z]/',' ', $requested)); // replace non-alphabets with space
$brand = trim(preg_replace('/[a-zA-Z]/',' ', $requested)); // replace non-numerics with space
$redirect_url = 'http://' . $domain . '/' . $brand;
header('Location:' . $redirect_url);
但是,如果您可以将域名和品牌作为两个单独的参数,并在重定向它们之前对它们进行单独清理,以防止从单个参数中提取它们的开销,那就更好了


注意:当域名本身有数字时,这个表达式可能是无用的,因为客户端是通过大量的验证获得的,并且实际上需要卫生条件。

如果出于某种目的您想使用explode,那么您需要有一个分隔符。
$brand = strtolower($_GET['Client'][0]);
$id    = substr($_GET['Client'], 1);

header("Location: http://{$brand}.com/{$id}");
让我们以“u”作为分隔符,因此您的示例如下:domain.com/script.php?Client=A_12345

$yourstring = explode("_",$_GET["Client"]);

echo $yourstring[0];
//will output A 
echo $yourstring[1];
//will output 12345

//your simple controller could be something like this
switch($yourstring[0]){
case: 'A':
    header('Location: http://a.com?id='.$yourstring[1]);
    exit();
    break;

case: 'B':
    header('Location: http://b.com?id='.$yourstring[1]);
    exit();
    break;

default:
//etc
}

做一个简单的字符串连接<代码>'位置:http://a.com/“.$id你在哪里买的这些域名:
a.com
b.com
?@bsdnoobz它们都是虚构的,只是举个例子:)这和我的问题很相似,但同样的问题是,它没有将id附加到URLAhhh的末尾,似乎我们都有相同的问题,fullcode应该是$exp=explode('A',$fullcode)中的fullcode;您定义$fullCode,然后在两个explode中使用$fullCode。PHP区分大小写。
$yourstring = explode("_",$_GET["Client"]);

echo $yourstring[0];
//will output A 
echo $yourstring[1];
//will output 12345

//your simple controller could be something like this
switch($yourstring[0]){
case: 'A':
    header('Location: http://a.com?id='.$yourstring[1]);
    exit();
    break;

case: 'B':
    header('Location: http://b.com?id='.$yourstring[1]);
    exit();
    break;

default:
//etc
}