Extract zipcodes from string and build a select statement in php -


I have a string that I capture with my DB that contains city and pincode information. I want to remove the zipcode (always 5 digits) from it and want to create an alternative statement (using PHP) as follows:

ZIP <02> 02118, 02116 ), Chelsea (02150), Rev. (02151) '; & Lt; Gt selection and; & Lt; Option value = "" & gt; Please select & lt; / Option & gt; & Lt; Option value = "02108" & gt; 02108 & lt; / Options & gt; & Lt; Option value = "02112" & gt; 02112 & lt; / Options & gt; & Lt; Option value = "02116" & gt; 02116 & lt; / Options & gt; & Lt; Option value = "02150" & gt; 02150 & lt; / Options & gt; & Lt; Option value = "02151" & gt; 02151 & lt; / Options & gt; & Lt; / Select & gt;

Please note that the string can have many towns, zododes, so that the solution is flexible enough to adjust it.

Try it out:

   

The above code exits everything except numbers and commas, then it explodes with commas, which gives you the necessary zipcode. Then loop through the zodiac array and select.

Code> & lt ;? Php $ townZip = 'Boston (02108, 02112, 02116), Chelsea (02150), Rev. (02151)'; $ Zips = Explosion (',', preg_replace ('# [^ A-Za-z, 0-9 () #', '', $ Town zip)); Echo '& lt; Select & gt; ' Echo '& lt; Option value = "" & gt; Please select & lt; / Option & gt; '; $ Prev = ''; Foreach ($ zip $ zip) {$ temp = explode ('(', $ zip); if (isset ($ temp [1]) $ {$ Prev = $ temp [0]; $ temp [0] = $ Temp [1];} echo '' & lt; option value = "'. $ Temp [0]." & Gt;'. $ Prev ''. $ Temp [0]. '& Lt; / options & gt; ; ';} Echo

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -