PHP:pregmatch输入字段

PHP:pregmatch输入字段,php,forms,Php,Forms,我有一个带有文本输入字段的html表单。我想知道如何识别表单中的特定输入。输入命令示例: <input type="text" name="action" value="bookmark http://google.com" /> <?php if ($command == "goto"): // go to website X elseif ($command == "bookmark"): // bookmar

我有一个带有文本输入字段的html表单。我想知道如何识别表单中的特定输入。输入命令示例:

<input type="text" name="action" value="bookmark http://google.com" />
<?php
        if ($command == "goto"):
        // go to website X
        elseif ($command == "bookmark"):
        // bookmark website X
        else:
        // something else
        endif;
?>

试试这个:

$request = $_POST['action'];

$split = explode(' ',$request,2);
$command = $split[0];

if(!isset($split[1])){
    //no url
    die;
}

$url = $split[1];

if($command == "goto"){

    header('location: '.$url);
    die;

}elseif($command == "bookmark"){

    header('location: '.$url);
    die;

}else{

    echo 'No Commands :(';

}
使用
$\u POST
$\u GET
检索请求数据。ie:
$\u GET['action']

设置标题位置以重定向浏览器<代码>模具
退出用于终止并输出当前脚本

$aAct = explode(' ', $_POST['action');
if(is_array($aAct)) {
    switch($aAct[0]) {
        case 'bookmark':
            /* do action e.g. header('Location: ' . $aAct[1]); */
        break;
    }
}
为您要指定的每个操作创建一个case/break组合。

类似的内容?:

//get the command from your value
$command = current(explode(" ", $_POST['action']));

//get the url from your value
$url     = next(explode(" ", $_POST['action']));
正如karim79所述,一个处理输入的开关更合适

switch($command) {

    case 'goto': 
         // do stuff with $url;
         break;
    case 'bookmark': 
         // do stuff with $url;
         break;
    default: // do something default;
}

希望对您有所帮助

我认为最简单的方法是在第一个空格中拆分字符串,将其拆分为一个命令和该命令的参数。explode()的“2”参数允许在$param中使用空格(如有必要)

$input = explode(' ', $_POST['action'], 2);
$command = $input[0];
$param = $input[1];

switch ($command) {
    case 'goto':
        // go to website $param
        break;
    case 'bookmark':
        // bookmark website $param
        break;
    default:
        // unknown command
}

$_POST['action']将永远不会是'goto',例如'goto{url}'