0

I'm trying to validate a UK postcode. How do I make use of the chosen answer in the following question via PHP?

RegEx for matching UK Postcodes

 [Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A- 
    Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?))))\s?[0-9][A-Za-z]{2}
theduck
  • 2,589
  • 13
  • 17
  • 23
user1228907
  • 369
  • 2
  • 6
  • 16
  • 1
    Just use it in the appropriate function. What is your issue? – Wiktor Stribiżew Nov 05 '19 at 13:14
  • 1
    You haven't copied the whole regex in the answer, it starts and ends with `(` and `)`, using that with e.g. `preg_match()` works just fine. – lovelace Nov 05 '19 at 13:23
  • I have tried the following and I get an error: preg_replace('([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?))))\s?[0-9][A-Za-z]{2})', $_POST['postcode'] ,$_POST['postcode']); – user1228907 Nov 05 '19 at 13:53
  • Warning: preg_replace(): Unknown modifier '|' – user1228907 Nov 05 '19 at 13:54
  • You need to use / as a delimiter inside the single quotes and outside the regular expression. For example `preg_replace('/([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?))))\s?[0-9][A-Za-z]{2})/', 'replacement' , $_POST['postcode']);` – Dan Winchester Nov 05 '19 at 16:05

0 Answers0