Replace multiple values

Hey, Im building a system where currently the database holds data with a number, eg: 305, 201…

I need these numbers replaced to some vehicle names, is there anyway that I can transform these numbers quickly without doing 1000s of string_replace?

So basicly I need the same consept as:

$vehicle = str_replace(“101”, Sultan);
$vehicle = str_replace(“102”, Infernus);

and so on, but theres about 300, an was hoping there is a much quicker way. Maybe a trick in wordpad or Notepad++ ?

str_replace can take arrays as it’s arguments.

At this moment I currently have two lists:

Blista Compact


They all match and they need to be set to each other, top one to the top one, and so on… I dont get what you mean…

“in a database”

Do you mean inside a text field in a table

“I was driving a 301 when I was overtaken by a 404…”

OR do you mean there is a column in a table holding the car number and there are thousands of them?

car_no | status
602 | 1
496 | 1
401 | 0
518 | 1
527 | 1
589 | 0
419 | 1
533 | 1
526 | 1
474 | 1

There is a table called Vehicles, where there is a field called “VehicleID”. I need to be able to display these vehicles, but by names.

I already know the names to the VehicleIDs, but it will be impossible for me to change it so that the field holds the name instead of the ID, so instead I just need to change it via PHP when its called up for display.

Where are the names currently stored?

There not, I have them in a text file on my PC. Basicly in easy terms, I need to do Str_replace 160 times, but dont want to type it out so much. I currently have a text file with:

vehicleid vehiclename
vehicleid vehiclename
vehicleid vehiclename
vehicleid vehiclename
vehicleid vehiclename

Do any vehicle names have spaces in them? On the assumption that no vehicles have any spaces in their names:

  1. Add a new field to the vehicles table for the vehicle names to go into.
  2. Use PHP’s file() function to read the text file into an array (each line in the file will go into a new element in the array.
  3. Using a foreach loop on the array:

foreach ($vehicles AS $vehicle_line) {
    $vehicle_name = explode(" ",$vehicle_line);

Then have php sort the $vehicle_list array into the same order that the vehicles are stored in the database. Then use another foreach loop on the $vehicle_list array to do an update (assuming the name field is called name and the table is called vehicle):

foreach ($vehicle_list AS $vehicle ) {
            car_id = {$vehicle['id']
    // run query

The WHERE clause is a must as you don’t want to overwrite the name each time. You’ll have to adjust the field and table names to match what they are in the database.

If any car names have got spaces in them, they you’ll have to eliminate them spaces (replace the spaces in the names with _ ) but be careful not to eliminate the spaces between the vehicle ID and the vehicle name.

Please not I’ve very little experience working with php and php accessing files (not referring to includes). Once the names are stored in the database then you’d just need to do a relevant SELECT query to get the names of whatever vehicles are required.

NB: Code not tested