Php 使用分隔符将ereg转换为preg_匹配

Php 使用分隔符将ereg转换为preg_匹配,php,Php,我发现我使用的一个脚本已经停止工作,因为php不再支持“ereg”…我自己没有编写这个脚本,但我一辈子都不知道在哪里放置分隔符 我已将“ereg”和“ereg_替换”分别更改为“preg_match”和“preg_替换” function checkPostcode($toCheck) { $orig = $toCheck; // Permitted letters depend upon their position in the postcode. $alp

我发现我使用的一个脚本已经停止工作,因为php不再支持“ereg”…我自己没有编写这个脚本,但我一辈子都不知道在哪里放置分隔符

我已将“ereg”和“ereg_替换”分别更改为“preg_match”和“preg_替换”

    function checkPostcode($toCheck) {



  $orig = $toCheck;



  // Permitted letters depend upon their position in the postcode.

  $alpha1 = "[abcdefghijklmnoprstuwyz]";                          // Character 1

  $alpha2 = "[abcdefghklmnopqrstuvwxy]";                          // Character 2

  $alpha3 = "[abcdefghjkstuw]";                                   // Character 3

  $alpha4 = "[abehmnprvwxy]";                                     // Character 4

  $alpha5 = "[abdefghjlnpqrstuwxyz]";                             // Character 5



  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA

  $pcexp[0] = '^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([0-9]{1}'.$alpha5.'{2})$';



  // Expression for postcodes: ANA NAA

  $pcexp[1] =  '^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$';



  // Expression for postcodes: AANA NAA

  $pcexp[2] =  '^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$';



  // Exception for the special postcode GIR 0AA

  $pcexp[3] =  '^(gir)(0aa)$';



  // Standard BFPO numbers

  $pcexp[4] = '^(bfpo)([0-9]{1,4})$';



  // c/o BFPO numbers

  $pcexp[5] = '^(bfpo)(c\/o[0-9]{1,3})$';



  // Load up the string to check, converting into lowercase and removing spaces

  $postcode = strtolower($toCheck);

  $postcode = str_replace (' ', '', $postcode);



  // Assume we are not going to find a valid postcode

  $valid = false;



  // Check the string against the six types of postcodes

  foreach ($pcexp as $regexp) {



    if (preg_ma($regexp,$postcode, $matches)) {



      // Load new postcode back into the form element  

      $toCheck = strtoupper ($matches[1] . ' ' . $matches [2]);



      // Take account of the special BFPO c/o format

      $toCheck = preg_replace ('C\/O', 'c/o ', $toCheck);



      // Remember that we have found that the code is valid and break from loop

      $valid = true;

      break;

    }

  }

任何帮助都将不胜感激。

分隔符应该包装正则表达式,因此必须将其放置在
$pcexp[0]
$pcexp[1]
中。delimeters是正则表达式模式字符串开头和结尾的一对字符。标准分隔符是
/
,但如果您愿意,可以使用其他分隔符

例如:

'^(bfpo)([0-9]{1,4})$'
应改为:

'/^(bfpo)([0-9]{1,4})$/'
 ^                    ^
added this         and this
如上所述,我在字符串的开头和结尾添加了一个
/
。如果愿意,您可以使用
#
~
或各种其他字符作为分隔符


请确保转义字符串中出现的任何分隔符字符,否则它将被视为结束分隔符。

谢谢…尝试过,但出现错误:警告:preg_replace():分隔符不能是字母数字或反斜杠鉴于收到的错误消息,您显然没有按照我说的做,因为它说你用一个字母或反斜杠作为分隔符。我指定的分隔符是正斜杠。PHP的错误消息说您没有这样做。如果你确定你做了,那么我想事情比你告诉我们的还要多。尝试在调用
preg\u match
之前立即回显regex模式,以查看实际使用的值。无论哪种方式,在这里进行一点调试工作都是最好的选择;脚本比需要的更复杂,但也不过分复杂。尝试使用
echo
对其进行跟踪,以查看发生了什么,查看它在哪里中断,以及使用了什么值。然后写一个小的测试程序,只做一小段代码,使用给定的值,并使用它,直到它工作。我使用的开发人员不在,我知道一点,但这超出了我的能力,这个脚本已经工作了好几年,但突然它停止工作,我有9个客户端网站。我敢肯定这与“ereg”不受支持有关……是否有我可以向您发送完整文件的方法,因为它可能很小?错误在于
preg\u replace()
调用。这也需要以相同的方式添加分隔符。将
'C\/O'
更改为
'/C\/O/'
。我在回答中给你的那句话就是一个例子。。。所有正则表达式模式都需要添加分隔符,而不仅仅是那个分隔符。