2013-09-16 74 views
0

我發現我使用的腳本已停止工作,因爲'ereg'不再支持PHP ...我沒有自己寫這個腳本,但不能爲我的生活弄清楚在哪裏放置分隔符。使用分隔符將ereg轉換爲preg_match

我已分別用'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; 

    } 

    } 

任何幫助將不勝感激。

回答

0

分隔符應包裝正則表達式,因此它必須放置在$pcexp[0]$pcexp[1]

0

定界符是正則表達式模式字符串開始和結束處的一對字符。標準分隔字符是/,但如果您願意,也可以使用其他字符。

因此,舉例來說:

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

應改爲:正如你看到的上面

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

,我添加了一個/字符串的開始和結束。如果您願意,您可以使用#~或各種其他字符作爲分隔符。

務必避免字符串中任何分隔字符的出現,否則它將被視爲結束分隔符。

+0

謝謝...嘗試過,但拋出錯誤:警告:preg_replace():分隔符不能是字母數字或反斜槓 –

+0

鑑於你有錯誤信息,你清楚*沒有*做我說的,因爲它是說你使用一個字母或反斜槓作爲分隔符。我指定的分隔符是* forward *斜槓。 – Spudley

+0

我試着添加/,*,# –

相關問題