PDA

View Full Version : URGENT: i need a regular expression


MrNase
Thu 6th May '04, 7:20am
I have a list of 260 entries which looks like the following example:


NUMBER CITY
208 Landau
209 Kiel
210 Aachen
211 Baden-Baden
212 Berlin
214 Dortmund
215 Mönchengladbach
216 Düsseldorf
217 Frankfurt
218 Fulda
219 Hamburg
220 Hannover
221 Köln, Leverkusen (new)
222 Wuppertal



Now i want to replace any number with:
case 'NUMBER':

And any city with:

$land= 'CITY';
break;


Now i need a regular expression for this.
My editor allows me to use it to search and replace.

This is really urgent because people are waiting for the end result ;)

Stadler
Thu 6th May '04, 8:30am
<?php

$text = file_get_contents('cities.txt');

$hits = preg_match_all('#^\s*(\d+)\s*(.*?)\s*$#m', $text, $matches);

for ($i = 0; $i < $hits; $i++)
{
echo "case: '{$matches[1][$i]}':\n\t\$land = '{$matches[2][$i]}';\n\tbreak;\n\n";
}

?>Something like this?

MrNase
Thu 6th May '04, 9:55am
exactly.

Thank you so much :)