Tools and Services Blog Learn Quizzes Smile API Log In / Sign Up
Tools and Services Blog Learn Quizzes Smile API Log In / Sign Up
« Return to the tutorials list
We have updated our privacy policy to let you know that we use cookies to personalise content and ads. We also use cookies to analyse our traffic and we share information about your use of our site and application with our advertising and analytics partners. By using this website or our application you agree to our use of cookies. Learn more about the way this website uses cookies or remove this message.

How to modify MARC records with File_MARC

November 25, 2014 Difficulty: 25 / 50 Tweet
marc-records-screen

One of my clients, a children e-books publisher, recently asked if I can help him modify MARC records for their library. As I had no idea what a MARC record is I started off here and soon I found that there's a PEAR package that can be used to read and write MARC files.

I will not go into what a MARC record is and what it is used for because if you are reading this article you probably already know that. In this article I will focus on how one can use the "File_MARC" PEAR package to modify/update the fields in a MARC file.

My client requested that I change the 856 field from the MARC record, which is the url pointing to the book inside their library. According to the Library of Congres the fancier definition for the 856 field is used for "electronic location and access information to an electronic resource".

Because the File_MARC package is not very generous when it comes to documentation, I practically had to do this using a combination of guessing and an overdose of ReflectionClass-based var_dumps. Here's the script I came up with in order to modify that field and write it in a new MARC record file (Don't forget to install the File_MARC package first!).

  
  <?php
    require 'File/MARC.php';
    $marc = new File_MARC('source_file.mrc'); //load the source marc file
    $modified = fopen( __DIR__ . "/records.mrc", "wb"); //load a new file where you will put the modified records - binary format

    // Iterate through the retrieved records
    while ($record = $marc->next()) {
        $url = $record->getField('856'); // alter the 856 field
        foreach ($url->getSubfields() as $key => $value) { //get the subfields for 856
            $isEmpty = $value->isEmpty();
            if(!$isEmpty)
              $value->setData("http://www.example.com"); // if the subfield is not empty modify it with the new URL
        }
        fwrite($modified, $record->toRaw()); //write the altered record to the file
    }
    fclose($modified); //close the file handle
  ?>
  
comments powered by Disqus