Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 希伯来语或英语正则表达式_Php_Regex_Preg Match_Hebrew - Fatal编程技术网

Php 希伯来语或英语正则表达式

Php 希伯来语或英语正则表达式,php,regex,preg-match,hebrew,Php,Regex,Preg Match,Hebrew,我正在寻找一个模式,只接受希伯来语或英语字母从2个字母到15个,可以接受1个空格。我尝试了以下代码,但它与我的字符串不匹配: <?php $subject = "שלום לך"; $regexp="#^\p[{Hebrew}| ][a-zA-Z]{2,15}? \+$#u"; print_r(preg_match($regexp, $subject)); ?> 怎么样?那对你有用吗 [\w\u0590-\u05FF] 您的代码中有多个错误 首先,你的正则表达式 $regexp

我正在寻找一个模式,只接受希伯来语或英语字母从2个字母到15个,可以接受1个空格。我尝试了以下代码,但它与我的字符串不匹配:

<?php
$subject = "שלום לך";
$regexp="#^\p[{Hebrew}| ][a-zA-Z]{2,15}? \+$#u";
print_r(preg_match($regexp, $subject));
?>

怎么样?那对你有用吗

[\w\u0590-\u05FF]

您的代码中有多个错误

首先,你的正则表达式

$regexp="#^\p[{Hebrew}| ][a-zA-Z]{2,15}? \+$#u";
这就是它的意思:

#                     : regex delimiter
  ^                   : begining of string
    \p                : character p
    [{Hebrew}| ]      : character class, one of the char : {, H, e, b, r, w, }, |, space 
    [a-zA-Z]{2,15}?   : from 2 to 15 alphabetic char
     \+               : a space followed by +
  $                   : end of string
#                     : regex delimiter
u                     : unicode
Unicode希伯来文字符是:
\p{hebrew}

char类中不需要
|

字符串中没有
+
,末尾没有空格
不需要进行不加密匹配

因此,应将其改写为:

$regexp="#^[\p{Hebrew} a-zA-Z]{2,15}$#u";
说明:

#                 : regex delimiter
  ^               : begining of string
    [             : start class character
      \p{Hebrew}  : a hebrew character
                  : a space
      a-zA-Z      : a latin letter
    ]             : end of class
    {2,15}        : previous chars 2 to 15 times
  $               : end of string
#                 : regex delimiter
u                 : unicode
不返回数组,而是一个int,用于保存在字符串中找到模式的时间

然后您的脚本变成:

$subject = "שלום לך";
$regexp  = "#^[\p{Hebrew} a-zA-Z]{2,15}$#u";
preg_match($regexp, $subject, $m);
print_r($m);

注-类似的正则表达式可用于正则表达式的其他方言,即C#/.Net。
var regexp = /^[\u0591-\u05F4\s]+$/gi;
return regexp.test(str)