php - How can I make sure the code runs all possibilities? -


i'm stuck!

the code looks words or sentences preg_match_all , replaces words , converts units.

long text becomes $lines[] through explode("\n", $long_text);

a loop runs through $lines.

if(preg_match_all) finds if line contains it's looking for.

problem occurs:

two of same preg_match_all() true, because code looks once on each line skips it.

other problem:

3500ft handled 500 ft because preg_match_all("/(\d{3,4})(ft)/", $lines[$i], $output_array) has option of 3 digits. how can force first 4 digits 3 digits. option 2 preg_matches first 1 {4} , next {3}?

example long_text:

isbildning område 1a,1b,1c,2a,2b,3c,4a: under hela perioden lätt till måttlig isbildning mellan 1500ft och fl090. område 3a,3b: början av perioden lätt till måttlig isbildning mellan fl060 och fl090.i mitten och mot slutet av perioden lätt till måttlig isbildning mellan 1500ft och fl090. område 4b: mot slutet av perioden lätt till måttlig isbildning mellan fl060 och fl090.

sikt/väder/moln område 2a: under hela perioden sikt över 8km, lokalt 3000-5000m snöfall. molnbas över 2000ft, lokalt 1500-2000ft. molnöversida fl125. område 1b: början av perioden sikt över 8km, lokalt under 1500m dis. molnbas över 2000ft, lokalt 500-1000ft. molnöversida >fl125. mitten och mot slutet av perioden sikt över 8km, lokalt 1500-3000m snöfall. molnbas över 2000ft, lokalt 1000-1500ft. molnöversida fl125. område 4b,4c: under hela perioden sikt över 8km. molnbas över 2000ft. molnöversida: ingen prognos. område 3a,3b: under hela perioden sikt över 8km. molnbas över 2000ft. molnöversida >fl125. område 1c,2b,3c,4a: början och mitten av perioden sikt över 8km, lokalt under 1500m dis. molnbas över 2000ft, lokalt 500-1000ft. molnöversida >fl125. mot slutet av perioden sikt över 8km, lokalt 3000-5000m snöfall. molnbas över 2000ft, lokalt 1500-2000ft. molnöversida >fl125. område 1a: under hela perioden sikt över 8km, lokalt 1500-3000m snöfall. molnbas över 2000ft, lokalt 1000-1500ft. molnöversida fl125.

for($i=0;$i<count($lines);$i++) {   if(preg_match_all("/(mellan) (\d{2}) (och) (\d{2}) (utc)/", $lines[$i], $output_array)){     $start = $output_array[1][0] . " " . $output_array[2][0] . " " . $output_array[3][0] . " " . $output_array[4][0] . " " . $output_array[5][0];     $goal =  $output_array[1][0] . " " . 1*($output_array[2][0]+$diff) . " " . $output_array[3][0] . " " . 1*($output_array[4][0]+$diff)  . " svensk tid";     $lines[$i] = str_replace($start, $goal, $lines[$i]);   }    if(preg_match_all("/(\d{3,4}) (fot)/", $lines[$i], $output_array)){     $start = $output_array[1][0] . " " . $output_array[2][0];     $output_array[1][0] = convertmeter($output_array[1][0]);     $goal =  $output_array[1][0] . " meter";     $lines[$i] = str_replace($start, $goal, $lines[$i]);   }    if(preg_match_all("/(\d{3,4}) (ft)/", $lines[$i], $output_array)){     $start = $output_array[1][0] . " " . $output_array[2][0];     $output_array[1][0] = convertmeter($output_array[1][0]);     $goal =  $output_array[1][0] . " meter";     $lines[$i] = str_replace($start, $goal, $lines[$i]);   }    if(preg_match_all("/(\d{3,4})-(\d{3,4})(ft)/", $lines[$i], $output_array)){     $start = $output_array[1][0] ."-" . $output_array[2][0] . "ft";     $output_array[1][0] = convertmeter($output_array[1][0]);     $output_array[2][0] = convertmeter($output_array[2][0]);     $goal =  $output_array[1][0] . "-" . $output_array[2][0] . " meter";     $lines[$i] = str_replace($start, $goal, $lines[$i]);   }    if(preg_match_all("/(\d{3,4})(ft)/", $lines[$i], $output_array)){     $start = $output_array[1][0] . $output_array[2][0];     $output_array[1][0] = convertmeter($output_array[1][0]);     $goal =  $output_array[1][0] . " meter";     $lines[$i] = str_replace($start, $goal, $lines[$i]);   }     if(preg_match_all("/(fl)(\d{2,3})/", $lines[$i], $output_array)){     $start = $output_array[1][0] . $output_array[2][0];     $output_array[2][0] = convertmeter($output_array[2][0]*100);     $goal =  $output_array[2][0] . " meter";     $lines[$i] = str_replace($start, $goal, $lines[$i]);   }    if(strpos($lines[$i], "&gt;") !== false) $lines[$i] = str_replace("&gt;", "över ", $lines[$i]); } 

i truely hate formatting on stackoverflow.

may present ugliest solution ever....

preg_match_all("/\b(\d+)(ft)/", $lines[$i], $output_array);                     $x = count($output_array[1]);                     for($d=0;$d<$x;$d++){                         if(preg_match_all("/\b(\d+)(ft)/", $lines[$i], $output_array)){                             $start = $output_array[1][0] . $output_array[2][0];                             $output_array[1][0] = convertmeter($output_array[1][0]);                             $goal =  $output_array[1][0] . " meter";                             $lines[$i] = preg_replace("/".$start."/", $goal, $lines[$i],1);                         }                      } 

first preg_match see if "value" exsists. count on array , start forloop on number of times preg_match found values.

it job, it's ugly.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -