Using Structures_BibTex

Lets suppose you got ten references from PubMed and saved them under "texmed.bib". The content of this BibTex File looks like this:


% 9606928 (JID)
  @Article{pmid16594765,
      Author="Maccallum, Robert C and Browne, Michael W and Cai, Li",
      Title="{Testing differences between nested covariance structure models: Power analysis and null hypotheses}",
      Journal="Psychol Methods",
      Year="2006",
      Volume="11",
      Number="1",
      Pages="19--35",
      Month="Mar"
      }	


% 0375356 (JID)
  @Article{pmid16606695,
      Author="Waterhouse, Nigel J and Sutton, Vivien R and Sedelies, Karin A and Ciccone, Annette and Jenkins, Misty and Turner, Stephen J and Bird, Phillip I and Trapani, Joseph A",
      Title="{Cytotoxic T lymphocyte-induced killing in the absence of granzymes A and B is unique and distinct from both apoptosis and perforin-dependent lysis}",
      Journal="J Cell Biol",
      Year="2006",
      Volume="173",
      Number="1",
      Pages="133--144",
      Month="Apr"
      }


% 101232529 (JID)
  @Article{pmid16594196,
      Author="Wein, Simon",
      Title="{The firmament of consciousness}",
      Journal="Palliat Support Care",
      Year="2005",
      Volume="3",
      Number="1",
      Pages="55",
      Month="Mar"
      }


% 101262774 (JID)
  @Article{pmid16566567,
      Author="Walker, James and Maccallum, Matt and Fischer, Carl and Kopcha, Robert and Saylors, Roy and McCall, John",
      Title="{Sedation using dexmedetomidine in pediatric burn patients}",
      Journal="J Burn Care Res",
      Year="2006",
      Volume="27",
      Number="2",
      Pages="206--210",
      Month="Mar"
      }


% 7603616 (JID)
  @Article{pmid16580511,
      Author="Henderson, Michael A",
      Title="{In reply to drs. Godinez and gombos}",
      Journal="Int J Radiat Oncol Biol Phys",
      Year="2006",
      Volume="64",
      Number="5",
      Pages="1611",
      Month="Apr",
      Note="Letter"
      }


% 7603509 (JID)
  @Article{pmid16597590,
      Author="Kinross KM and Clark AJ and Iazzolino RM and Humbert PO",
      Title="{E2f4 regulates fetal erythropoiesis through the promotion of cellular proliferation}",
      Journal="Blood",
      Year="2006",
      Month="Apr",
      Note="JOURNAL ARTICLE"
      }


% 8205768 (JID)
  @Article{pmid16598744,
      Author="Thomas D and Kansara M",
      Title="{Epigenetic modifications in osteogenic differentiation and transformation}",
      Journal="J Cell Biochem",
      Year="2006",
      Month="Apr",
      Note="JOURNAL ARTICLE"
      }


% 0374236 (JID)
  @Article{pmid16565958,
      Author="Jefford M",
      Title="{Factors associated with interval adherence to mammography screening in a population-based sample of New Hampshire women}",
      Journal="Cancer",
      Year="2006",
      Month="Mar",
      Note="JOURNAL ARTICLE"
      }


% 101232529 (JID)
  @Article{pmid16594228,
      Author="Wein, Simon",
      Title="{Death of a generation}",
      Journal="Palliat Support Care",
      Year="2003",
      Volume="1",
      Number="4",
      Pages="381--383",
      Month="Dec"
      }


% 100927353 (JID)
  @Article{pmid16608535,
      Author="Sloan EK and Pouliot N and Stanley KL and Chia J and Moseley JM and Hards DK and Anderson RL",
      Title="{Tumor-specific expression of alphavbeta3 integrin promotes spontaneous metastasis of breast cancer to bone}",
      Journal="Breast Cancer Res",
      Year="2006",
      Volume="8",
      Number="2",
      Pages="R20",
      Month="Apr",
      Note="JOURNAL ARTICLE"
      }
So you got different Types with different entries and some comments between the entries. To get this BibTex data in a PHP Array you would do something like this:


<?php
require_once 'Structures_BibTex.php';

$foo=new Structures_BibTex();
$ret=$foo->loadFile('texmed.bib');
if(PEAR::isError($ret)) {
    print $ret->getMessage();
    die();
 }
if(PEAR::isError($ret=$foo->parse())) {
    print $ret->getMessage();
 } else {
    print_r($foo->data);
    print "---------------------------------------\n";
    print "Warnings:\n";
    print_r($foo->warnings);
 }
?>
The result would be:


Array
(
    [0] => Array
        (
            [month] => Mar
            [pages] => 19--35
            [number] => 1
            [volume] => 11
            [year] => 2006
            [journal] => Psychol Methods
            [title] => Testing differences between nested covariance structure models: Power analysis and null hypotheses
            [author] => Array
                (
                    [0] => Maccallum, Robert C
                    [1] => Browne, Michael W
                    [2] => Cai, Li
                )

            [cite] => pmid16594765
            [type] => article
        )

    [1] => Array
        (
            [month] => Apr
            [pages] => 133--144
            [number] => 1
            [volume] => 173
            [year] => 2006
            [journal] => J Cell Biol
            [title] => Cytotoxic T lymphocyte-induced killing in the absence of granzymes A and B is unique and distinct from both apoptosis and perforin-dependent lysis
            [author] => Array
                (
                    [0] => Waterhouse, Nigel J
                    [1] => Sutton, Vivien R
                    [2] => Sedelies, Karin A
                    [3] => Ciccone, Annette
                    [4] => Jenkins, Misty
                    [5] => Turner, Stephen J
                    [6] => Bird, Phillip I
                    [7] => Trapani, Joseph A
                )

            [cite] => pmid16606695
            [type] => article
        )

    [2] => Array
        (
            [month] => Mar
            [pages] => 55
            [number] => 1
            [volume] => 3
            [year] => 2005
            [journal] => Palliat Support Care
            [title] => The firmament of consciousness
            [author] => Array
                (
                    [0] => Wein, Simon
                )

            [cite] => pmid16594196
            [type] => article
        )

    [3] => Array
        (
            [month] => Mar
            [pages] => 206--210
            [number] => 2
            [volume] => 27
            [year] => 2006
            [journal] => J Burn Care Res
            [title] => Sedation using dexmedetomidine in pediatric burn patients
            [author] => Array
                (
                    [0] => Walker, James
                    [1] => Maccallum, Matt
                    [2] => Fischer, Carl
                    [3] => Kopcha, Robert
                    [4] => Saylors, Roy
                    [5] => McCall, John
                )

            [cite] => pmid16566567
            [type] => article
        )

    [4] => Array
        (
            [note] => Letter
            [month] => Apr
            [pages] => 1611
            [number] => 5
            [volume] => 64
            [year] => 2006
            [journal] => Int J Radiat Oncol Biol Phys
            [title] => In reply to drs. Godinez and gombos
            [author] => Array
                (
                    [0] => Henderson, Michael A
                )

            [cite] => pmid16580511
            [type] => article
        )

    [5] => Array
        (
            [note] => JOURNAL ARTICLE
            [month] => Apr
            [year] => 2006
            [journal] => Blood
            [title] => E2f4 regulates fetal erythropoiesis through the promotion of cellular proliferation
            [author] => Array
                (
                    [0] => Kinross KM
                    [1] => Clark AJ
                    [2] => Iazzolino RM
                    [3] => Humbert PO
                )

            [cite] => pmid16597590
            [type] => article
        )

    [6] => Array
        (
            [note] => JOURNAL ARTICLE
            [month] => Apr
            [year] => 2006
            [journal] => J Cell Biochem
            [title] => Epigenetic modifications in osteogenic differentiation and transformation
            [author] => Array
                (
                    [0] => Thomas D
                    [1] => Kansara M
                )

            [cite] => pmid16598744
            [type] => article
        )

    [7] => Array
        (
            [note] => JOURNAL ARTICLE
            [month] => Mar
            [year] => 2006
            [journal] => Cancer
            [title] => Factors associated with interval adherence to mammography screening in a population-based sample of New Hampshire women
            [author] => Array
                (
                    [0] => Jefford M
                )

            [cite] => pmid16565958
            [type] => article
        )

    [8] => Array
        (
            [month] => Dec
            [pages] => 381--383
            [number] => 4
            [volume] => 1
            [year] => 2003
            [journal] => Palliat Support Care
            [title] => Death of a generation
            [author] => Array
                (
                    [0] => Wein, Simon
                )

            [cite] => pmid16594228
            [type] => article
        )

    [9] => Array
        (
            [note] => JOURNAL ARTICLE
            [month] => Apr
            [pages] => R20
            [number] => 2
            [volume] => 8
            [year] => 2006
            [journal] => Breast Cancer Res
            [title] => Tumor-specific expression of alphavbeta3 integrin promotes spontaneous metastasis of breast cancer to bone
            [author] => Array
                (
                    [0] => Sloan EK
                    [1] => Pouliot N
                    [2] => Stanley KL
                    [3] => Chia J
                    [4] => Moseley JM
                    [5] => Hards DK
                    [6] => Anderson RL
                )

            [cite] => pmid16608535
            [type] => article
        )

)
---------------------------------------
Warnings:
Array
(
)
As you can see, every entry has been parsed correctly and no warning has been fired. The first thing in the PHP source we create an instance of Structrues_BibTex. Then we try to read the content of a file. In the class the BibTex data is stored in content. This may be set externally, so there is no need to get that from a file, this can also be for example from an input field. Then the content is parsed. This may break and therefore it is checked for errors. Of course this will no happen here. The parsed data is stored in data and this is simply printed. After that, just in case, the warnings are printed.

Now an example which will fire every known Warning. The BibTex Data is stored in ill.bib and looks like this:


The first example has an At inside braces
@article{ill1,
    author="Elmar Pitschke",
    foo={Where does the @ belong?}
}

The next example illustrates the excaped double quotes warning
@article{ill2,
    author="Elmar Pitschke",
    foo="Here it \" is",
    }

The next one is tricky - unbalanced amount of braces in entry
@article{ill3,
    author="Elmar Pitschke",
    foo="Here it { is",
    bar="And here the } second"
    }

And finally we define the second identifiaction again
@article{ill2,
    author="Elmar Pitschke",
    foo="This one is correct",
    }
Using the same PHP script as above the output would look something like this:


Array
(
    [0] => Array
        (
            [foo] => Where does the @ belong?
            [author] => Array
                (
                    [0] => Elmar Pitschke
                )

            [cite] => ill1
            [type] => article
        )

    [1] => Array
        (
            [foo] => Here it \" is
            [author] => Array
                (
                    [0] => Elmar Pitschke
                )

            [cite] => ill2
            [type] => article
        )

    [2] => Array
        (
            [bar] => And here the } second
            [foo] => Here it { is
            [author] => Array
                (
                    [0] => Elmar Pitschke
                )

            [cite] => ill3
            [type] => article
        )

    [3] => Array
        (
            [foo] => This one is correct
            [author] => Array
                (
                    [0] => Elmar Pitschke
                )

            [cite] => ill2
            [type] => article
        )

)
---------------------------------------
Warnings:
Array
(
    [0] => Array
        (
            [warning] => WARNING_AT_IN_BRACES
            [entry] => {Where does the @ belong?}
            [wholeentry] => @article{ill1,
    author="Elmar Pitschke",
    foo={Where does the @ belong?}

        )

    [1] => Array
        (
            [warning] => WARNING_ESCAPED_DOUBLE_QUOTE_INSIDE_DOUBLE_QUOTES
            [entry] => "Here it \" is"
            [wholeentry] => @article{ill2,
    author="Elmar Pitschke",
    foo="Here it \" is",
    
        )

    [2] => Array
        (
            [warning] => WARNING_UNBALANCED_AMOUNT_OF_BRACES
            [entry] => "And here the } second"
            [wholeentry] => @article{ill3,
    author="Elmar Pitschke",
    foo="Here it { is",
    bar="And here the } second"
    
        )

    [3] => Array
        (
            [warning] => WARNING_UNBALANCED_AMOUNT_OF_BRACES
            [entry] => "Here it { is"
            [wholeentry] => @article{ill3,
    author="Elmar Pitschke",
    foo="Here it { is",
    bar="And here the } second"
    
        )

    [4] => Array
        (
            [warning] => WARNING_MULTIPLE_ENTRIES
            [entry] => ill2
            [wholeentry] => 
        )

)
As you can see every entry is parsed correctly but still there are some warnings that you should usually look after. Another possibility to use Structures_BibTex is to add another entry and export it to BibTex. We suppose you have a BibTex File called yourfile.bib and want to add some content to it. Then you would do something like this:


<?php
require_once 'Structures_BibTex.php';

$foo=new Structures_BibTex();
$ret=$foo->loadFile('yourfile.bib');
if(PEAR::isError($ret)) {
    print $ret->getMessage();
    die();
 }
if(PEAR::isError($ret=$foo->parse())) {
    print $ret->getMessage();
} else {
    $add=array();
    $add['type']='article';
    $add['author'][]='John Doe';
    $add['author'][]='Jane Doe';
    $add['title']='Foo Bar';
    $add['cite']='foo1';
    $foo->addEntry($add);
    print $foo->bibTex();
}
?>
The constructor has two possible parameter. Both are boolean values to set two modes. The first one whether the delimiters should be stripped from the values and the second whether warnings should be fired. Both are set to true per default. And here is another example if you want to see this class live in action.

Elmar Pitschke 2006-07-28