0

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

场景是我想从 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

提前致谢!

4

7 回答 7

1
$fullCode = $_REQUEST['Client'];
if(strpos($fullCode, 'A') !== false) {
   $exp = explode('A',$fullcode);
   header('Location: http://a.com/' . $exp[1]);
}
else if(strpos($fullCode, 'B') !== false) {
   $exp = explode('B',$fullcode);
   header('Location: http://b.com/' . $exp[1]);
}
else {
   die('No letter occurence');
}
于 2012-05-30T11:21:53.143 回答
0

如果出于某种目的您想使用爆炸,那么您需要有一个分隔符。让我们以“_”作为分隔符,因此您的示例将是这样的: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
}
于 2012-05-30T12:03:44.670 回答
0
$brand = strtolower($_GET['Client'][0]);
$id    = substr($_GET['Client'], 1);

header("Location: http://{$brand}.com/{$id}");
于 2012-05-30T11:48:08.263 回答
0

你可以轻松做到,

$value = $_GET['Client'];

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

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

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

于 2012-05-30T11:19:14.720 回答
0

你的意思是这样吗?

注意:
这仅适用于品牌只有 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");
}
?>
于 2012-05-30T11:19:31.983 回答
0

尝试使用:

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]}');
}
于 2012-05-30T11:24:30.430 回答
0

建议你也可以试试

$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);

但是,如果您可以将域名和品牌作为两个单独的参数获取并在重定向它们之前单独清理它们以防止从单个参数中提取它们的开销,那就更好了。

注意:当域名本身有数字时,此表达式可能无用,因为客户端是通过大量验证获得的,并且在现实中需要进行卫生处理。

于 2012-05-30T11:27:40.363 回答