How to use metaphone function to compare data in PHP

I was working on a project which required for me to scrape data from websites in order to feed our client’s comparison engine. Once the scraping was complete I then had to map the data to the data structure of the table in the database. For this reason I had to write a script which would take the data from the CSV and map it to the data in the database.

Now the problem was that there were minor differences in the data fetched from those websites. For e.g. I had the following data in my database table

Toyota Corolla Axio
Toyota Corolla Fielder
Toyota Landcruiser Prado

where as the data I had downloaded had the following entries

Toyota Corolla-Axio
Toyota CorolaAxio
Toyota Corolla-Fielder
Toyota Land Cruiser Prado
Toyota LandCruiser-Prado

and the list goes on.

I could not save this data correctly in the database as it could bring inconsistency in the data. Of course I could make changes in the script to read every character in the string and then compare with the best possible matches but there was still a chance of data getting incorrectly inserted.

So I used the metaphone() PHP function to compare the metaphone return value of both strings and then save the data into the table.

if (metaphone($strVarFromCSV) == metaphone($strVarFromDB)) {
 // save into database

After making the change mentioned above my data was correctly saved into the database and there were no in discrepancies.

If you know a better way then please share.

Be the first to comment

Leave a Reply