【问题标题】:invalid regular expression flag n无效的正则表达式标志 n
【发布时间】:2011-08-12 14:56:41
【问题描述】:

我正在尝试使用以下代码从 kohana 3 中的 url 获取参数,但是 fire bug 给了我一条错误消息“无效的正则表达式标志 n” 代码是:

控制器

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Test extends Controller_Template
{
public function action_index()
{
    $this->template = View::factory('par');
    $this->template->content = View::factory('par');
}
public function get()
{
    $param1 = $this->request->param('param1');
    $param2 = $this->request->param('param2');
    $param3 = $this->request->param('param3');

    echo "This is param1: ".$param1; 
    echo "This is param2: ".$param2; 
    echo "This is param3: ".$param3;
}
}

查看

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org   /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>


<?php $x=1;
  $y=2;
  $z=3;
?>

<input type="button" value = "button" onClick=<?php echo url::site('test/get'.$x);?> />
</body>
</html>

引导带

Route::set('default', '(<controller>(/<action>(/<param1>)(/<param2>)(/<param3>)))',
array(
    'param1' => '\d+',
    'param2' => '\d+',
    'param3' => '\d+',
))
->defaults(array(
    'controller' => 'test',
    'action'     => 'index',
));

请帮我解决这个问题。

【问题讨论】:

  • 浏览器中不显示任何内容....只是在firebug中显示以下行:无效的正则表达式标志n [Break On This Error] /param/index.php/test/get1 还有我我正在尝试获取所有三个参数...请告诉我该怎么做

标签: php kohana-3


【解决方案1】:

浏览器看起来像这样:

onClick=/param/index.php

onClick 属性被解释为 JavaScript,而 JavaScript 中的 /param/index.php 试图成为正则表达式文字,后跟属性访问。正则表达式将是 /param/,带有修饰符 indexi 表示不区分大小写的匹配,以便通过;那么你会出错,因为 JavaScript 正则表达式没有 n 修饰符。

我认为您正在尝试使按钮充当链接,因此您需要添加一些引号和对window.location 的赋值:

<input type="button" value = "button" onClick="window.location = '<?php echo url::site('test/get'.$x);?>'" />

这应该让你更接近你想要去的地方,或者至少让你克服神秘的“无效的正则表达式标志 n”错误。

【讨论】:

  • 非常感谢...这解决了我的问题并获得了三个参数。
猜你喜欢
  • 1970-01-01
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
  • 2020-04-12
  • 2020-11-14
  • 2018-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多