# Address cleanse

`addressCleanse()` matches a freeform UK address to the closest address on file and scores the match. Use it to clean addresses you already hold, such as a CRM export.

cleanse.phpphp

```php
use IdealPostcodes\IdealPostcodes;
use IdealPostcodes\Types\GbrCleanseMatch;

$client = IdealPostcodes::client('ak_test');
$result = $client->addressCleanse(['query' => '9 Malyons Rd, Swanley, BR8 7RE'])->result;

if ($result instanceof GbrCleanseMatch && $result->confidence > 0) {
    echo $result->match->line1, ', ', $result->match->postcode, ' (', $result->confidence, ')', PHP_EOL;
} else {
    echo 'No match', PHP_EOL;
}
```

## How do I tell a match from a no-match?

Check the class and the score. A match is a `GbrCleanseMatch` with a `confidence` above 0 and the matched `Address` in `match`. A no-match is a `GbrCleanseNoMatch`, with every score at 0, `match` empty and every match level at `NO_MATCH`.

## Can I send the postcode separately?

Yes. If your records keep the postcode or post town in their own fields, add them to the body as `postcode` and `post_town` alongside `query` rather than joining them into it. The second argument takes the query parameters: `tags`, `context` and `remove_organisation`.

## How do I cleanse an address outside the UK?

Pass `context` in the second argument with the country's ISO 3166-1 alpha-3 code. Cleanse defaults to the UK. The address datasets your key is licensed for decide which countries it can cleanse.

cleanse-usa.phpphp

```php
$result = $client->addressCleanse(
    ['query' => '1600 Pennsylvania Avenue NW, Washington, DC 20500'],
    ['context' => 'usa'],
)->result;
```

The result has the same shape for every country, so a match elsewhere is still a `GbrCleanseMatch` and the check above works unchanged.

## How do I leave the organisation out of the address lines?

Pass `remove_organisation` as `'true'` in the second argument. The organisation and department names are dropped from the address lines and the remaining lines move up. Use it when you deliver to people rather than businesses.

cleanse-lines.phpphp

```php
$result = $client->addressCleanse(
    ['query' => '10 Downing Street, London, SW1A 2AA'],
    ['remove_organisation' => 'true'],
)->result;

// $result->match->line1 is "10 Downing Street", not the organisation
```

`organisationName`, `departmentName` and the `native` record are unchanged. If removing the names would leave no address lines, they are kept.

## What confidence score should I accept?

It depends on your data. Each wrong, missing or misspelt element lowers the score, and one source tends to repeat the same mistakes. Cleanse a sample, sort it by `confidence` and set your threshold where the matches start to go wrong.

The result also scores each element separately, in `organisationMatch`, `premiseMatch`, `thoroughfareMatch`, `localityMatch`, `postTownMatch` and `postcodeMatch`. The [API reference](/docs/api/address-cleanse.md) explains each level.

## What does a cleanse cost?

One lookup for a match. A no-match costs nothing.

See [`cleanse`](/docs/sdks/php/endpoints/address-cleanse.md) for the body and parameters.
