CodeIgniter购物车类阿拉伯语正则表达式

时间:2012-11-22 14:25:57

标签: php codeigniter unicode

系统/库下的Cart.php有一个正则表达式模式定义,无法使用阿拉伯语作为名称值。这有效:

$data = array(
  'id' => "221212",
  'qty' => 1,
  'price' => 21.2,
  'name' => 'dasdasdas'
);

但由于名称中含有阿拉伯语,因此失败了:

$data = array(
  'id' => "221212",
  'qty' => 1,
  'price' => 21.2,
  'name' => 'عمر'
);

现在在Cart.php类中,我发现了以下内容:

// These are the regular expression rules that we use to validate the product ID and product name
  var $product_id_rules = '\.a-z0-9_-';
  // alpha-numeric, dashes, underscores, or periods
  var $product_name_rules = '\.\:\-_a-z0-9';
  // alphanumeric, dashes, underscores, colons or periods

我关注名称规则。显然这是问题,因为稍后会有一个检查:

if ( ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name'])) {
  log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces');
  return FALSE;
}

如何替换名称规则字符串以使用阿拉伯语?我的正则表达式背景很差,所以请帮帮忙。

谢谢!

enter image description here

3 个答案:

答案 0 :(得分:0)

如果您将$product_name_rules(部分)模式更改为:

,它将起作用
var $product_name_rules = '\.\:\-_a-z0-9\p{Arabic}';

...然后将/u修饰符添加到preg_match实际使用的模式中:

if ( ! preg_match("/^[".$this->product_name_rules."]+$/iu", 
            $items['name'])) { ... }

引用doc

  

u(PCRE_UTF8)

     

此修饰符打开PCRE的其他功能   这与Perl不兼容。模式字符串被视为UTF-8。   在Unix上可以从PHP 4.1.0或更高版本获得此修饰符   从win32上的PHP 4.2.3开始。从PHP 4.3.5开始检查模式的UTF-8有效性。

答案 1 :(得分:0)

如果您想接受其他字符,则必须在加载购物车库后修改$ this-> cart-> product_name_rules。

$this->load->library('cart');
$this->cart->product_name_rules = '\.a-z0-9_-\p{Arabic}';

答案 2 :(得分:0)

product_name正则表达式更改为以下内容,允许全部:

var $product_name_rules = '^.'