问题
The Cart.php under system/library has a regex pattern definition that is not enabling me to use Arabic for name values. This works:
$data = array(
'id' => "221212",
'qty' => 1,
'price' => 21.2,
'name' => 'dasdasdas'
);
But this fails because of Arabic in the name:
$data = array(
'id' => "221212",
'qty' => 1,
'price' => 21.2,
'name' => 'عمر'
);
Now in the Cart.php class, I found the following:
// 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
I am concerned with the name rules. Clearly this is the problem because later on there is a check:
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;
}
How can I replace the name rules string to work with Arabic? I have a very poor background with regex, so please help out.
Thanks!

回答1:
If you want to accept other characters, you have to modify $this->cart->product_name_rules after you have loaded the cart library.
$this->load->library('cart');
$this->cart->product_name_rules = '\.a-z0-9_-\p{Arabic}';
回答2:
It will work, if you change your $product_name_rules
(partial) pattern into this:
var $product_name_rules = '\.\:\-_a-z0-9\p{Arabic}';
... then add /u
modifier to the pattern actually used by preg_match
:
if ( ! preg_match("/^[".$this->product_name_rules."]+$/iu",
$items['name'])) { ... }
Quoting the doc:
u (PCRE_UTF8)
This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern strings are treated as UTF-8. This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern is checked since PHP 4.3.5.
回答3:
Change product_name
regex to the following, which allows all:
var $product_name_rules = '^.'
来源:https://stackoverflow.com/questions/13514736/codeigniter-cart-class-arabic-regex