2011-12-20 95 views
0

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

<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; 
?> 

回答

2

我认为最简单的方法就是分裂第一空间将其分离成一个命令和该命令的参数字符串。如果需要,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 
} 
+0

感谢您的回答! – 2011-12-20 22:53:43

0

尝试这种情况:

$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 :('; 

} 

$_POST使用或$_GET以检索所述请求的数据。即:$_GET['action']

设置标题位置以重定向浏览器。 die;exit;用于终止和输出电流脚本

+0

$ _ POST [“行动”]将永远不会“转到”,但“转到{URL}”例如 – 2011-12-20 13:11:31

+0

更新,以适应:)一点点额外的 – 2011-12-20 13:24:38

+0

感谢更新! – 2011-12-20 22:54:04

0
$aAct = explode(' ', $_POST['action'); 
if(is_array($aAct)) { 
    switch($aAct[0]) { 
     case 'bookmark': 
      /* do action e.g. header('Location: ' . $aAct[1]); */ 
     break; 
    } 
} 

制作的情况下/休息组合为你打算指定的每一个动作..

0

像这样的事情?:

//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; 
} 

希望它有助于