Test HTTP Requests Tools Blog Learn Quizzes Smile API Log In / Sign Up
Test HTTP Requests Tools Blog Learn Quizzes Smile API Log In / Sign Up
« Return to the tutorials list
We have updated the website and our policies to make sure your privacy rights and security are respected.
Click here to learn more about the way our website handles your data.

Remove this message.

How to modify MARC records with File_MARC

Daniel Gheorghe Difficulty: 25 / 50 Tweet

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!).

    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();
              $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