Filter postcode en aanpassen

Gestart doorRemcoboy4 reacties57 weergaven

5 berichten · pagina 1 van 1

Remcoboy

Lid · Topicstarter

Berichten
4.749
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
In een grote string staat de postcode fout. Afhankelijk van het land moet deze worden aangepast:9120BEVEREN in 9120,BEVEREN6814 EEARNHEM in 6814 EE,ARNHEM
Alle belgische postcodes zit de plaatsnaam dus vast aan het getal (altijd 4-cijferig), bij NL zit de plaatsnaam er ook aan vast, na een spatie en de 2 letters.

Het script moet dus uit de grote string de postcodes vissen (te herkennen aan de tag) en hier het format van aanpassen. Hoe doe ik dit?
0
Berichten
35.796
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
[COLOR= #0000BB]<?php
$string[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]<postcode>9120BEVEREN</postcode>
<postcode>6814 EEARNHEM</postcode>[/COLOR][COLOR= #007700];[/COLOR]

[COLOR= #0000BB]$array[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/explode"]explode[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"n"[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$string[/COLOR][COLOR= #007700]);[/COLOR]

[COLOR= #0000BB]$newstring[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000][/COLOR][COLOR= #007700];[/COLOR]

[URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$array[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$element[/COLOR][COLOR= #007700])
{[/COLOR]
    
    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/substr"]substr[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$element[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]10[/COLOR][COLOR= #007700]) ==[/COLOR] [COLOR= #DD0000]<postcode>[/COLOR][COLOR= #007700])
    {[/COLOR]
    
        [COLOR= #0000BB]$pos[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/strstr"]strstr[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$element[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000] [/COLOR][COLOR= #007700]) ?[/COLOR] [COLOR= #0000BB]17[/COLOR] [COLOR= #007700]:[/COLOR] [COLOR= #0000BB]14[/COLOR][COLOR= #007700];[/COLOR]
        
        [COLOR= #0000BB]$newstring[/COLOR] [COLOR= #007700].=[/COLOR] [URL="http://www.php.net/substr"]substr[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$element[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$pos[/COLOR][COLOR= #007700]).[/COLOR][COLOR= #DD0000], [/COLOR][COLOR= #007700].[/COLOR][URL="http://www.php.net/substr"]substr[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$element[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$pos[/COLOR][COLOR= #007700]).[/COLOR]PHP_EOL[COLOR= #007700];
        
    }
    
}[/COLOR]

[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$newstring[/COLOR][COLOR= #007700];[/COLOR]


Werkt trouwens alleen als elke postcode op een nieuwe regel staat.
0
Remcoboy

Lid · Topicstarter

Berichten
4.749
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Lennard schreef:
[COLOR= #0000BB]<?php
$string[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]<postcode>9120BEVEREN</postcode>
<postcode>6814 EEARNHEM</postcode>[/COLOR][COLOR= #007700];[/COLOR]

[COLOR= #0000BB]$array[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/explode"]explode[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"n"[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$string[/COLOR][COLOR= #007700]);[/COLOR]

[COLOR= #0000BB]$newstring[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000][/COLOR][COLOR= #007700];[/COLOR]

[URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$array[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$element[/COLOR][COLOR= #007700])
{[/COLOR]
    
    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/substr"]substr[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$element[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]10[/COLOR][COLOR= #007700]) ==[/COLOR] [COLOR= #DD0000]<postcode>[/COLOR][COLOR= #007700])
    {[/COLOR]
    
        [COLOR= #0000BB]$pos[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/strstr"]strstr[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$element[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000] [/COLOR][COLOR= #007700]) ?[/COLOR] [COLOR= #0000BB]17[/COLOR] [COLOR= #007700]:[/COLOR] [COLOR= #0000BB]14[/COLOR][COLOR= #007700];[/COLOR]
        
        [COLOR= #0000BB]$newstring[/COLOR] [COLOR= #007700].=[/COLOR] [URL="http://www.php.net/substr"]substr[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$element[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$pos[/COLOR][COLOR= #007700]).[/COLOR][COLOR= #DD0000], [/COLOR][COLOR= #007700].[/COLOR][URL="http://www.php.net/substr"]substr[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$element[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$pos[/COLOR][COLOR= #007700]).[/COLOR]PHP_EOL[COLOR= #007700];
        
    }
    
}[/COLOR]

[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$newstring[/COLOR][COLOR= #007700];[/COLOR]
Werkt trouwens alleen als elke postcode op een nieuwe regel staat.

Werkt in jouw voorbeeld inderdaad goed, echter is mijn string anders.

<straat>tekst</straat><postcode>2522 ZZDEN HAAG</postcode><land>NEDERLAND</land>heelwatzooi<postcode>3925SCHERPENZEEL</postcode><land>NEDERLAND</land>nogmeerzooi


Het is de bedoeling dat ie hierin de postcodes aanpast. Waarde van $newstring moet dus worden:

<straat>tekst</straat><postcode>2522 ZZ,DEN HAAG</postcode><land>NEDERLAND</land>heelwatzooi<postcode>3925,SCHERPENZEEL</postcode><land>NEDERLAND</land>nogmeerzooi


Het is dus de bedoeling dat de bestaande string aangepast wordt, niet dat enkel de postcodes eruit gehaald worden
0
Berichten
35.796
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Maar Scherpenzeel ligt ook in Nederland en daar heb je geen letters bij?
0
Remcoboy

Lid · Topicstarter

Berichten
4.749
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Lennard schreef:
Maar Scherpenzeel ligt ook in Nederland en daar heb je geen letters bij?

Dat was even als voorbeeld.

Met dank aan Rogier heb ik nu dit:

Codeblok (postcode.php):
1
2
3
4
5
6
7
8
9
10
11
12

$string = tekst2522 ZZDEN HAAGNEDERLANDheelwatzooi3925SCHERPENZEELNEDERLANDnogmeerzooi;

$array = array(
/([0-9]{4}?)([A-Z ]*)/ => $1,$2,
/([0-9]{4}) ([A-Z]{2})([A-Z ]*)/ => $1 $2,$3
);
$result= preg_replace(array_keys($array), array_values($array), $string);

echo $result;

?>

Echter pakt hij de NL nu niet meer goed vanwege de BE postcodes.

EDIT: Omdraaien van NL/BE hielp. Tnx rogier!
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren