0

我发现我使用的脚本已停止工作,因为 php 不再支持“ereg”...我自己没有编写此脚本,但我一生都无法弄清楚将分隔符放在哪里。

我已经分别用 'preg_match' 和 'preg_replace' 改变了 'ereg' 和 'ereg_replace'。

    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;

    }

  }

任何帮助将不胜感激。

4

2 回答 2

0

定界符应该包装正则表达式,所以它必须放在$pcexp[0]and中$pcexp[1]

于 2013-09-16T15:17:09.537 回答
0

分隔符是正则表达式模式字符串开头和结尾的一对字符。标准分隔符是/,但如果您愿意,也可以使用其他分隔符。

因此,例如:

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

应改为:

'/^(bfpo)([0-9]{1,4})$/'
 ^                    ^
added this         and this

正如你在上面看到的,我/在字符串的开头和结尾添加了一个。如果您愿意,可以使用#or~或各种其他字符作为分隔符。

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

于 2013-09-16T15:30:33.313 回答