removed dangling </table>
[openemr.git] / library / classes / RXList.class.php
blobf7db654da3b4c09076d456dce8e3fc61c78cd8a0
1 <?php
2 // $Id$
3 // $Author$
4 //
5 // $Log$
6 // Revision 1.3 2007/06/19 02:34:45 sunsetsystems
7 // drug lookup fix from Sam Rajan
8 //
9 // Revision 1.2 2005/12/27 00:45:19 sunsetsystems
10 // fix broken url
12 // Revision 1.1.1.3 2005/06/23 05:25:49 drbowen
13 // Initial import.
15 // Revision 1.1.1.2 2005/03/28 00:44:54 drbowen
16 // Initial import.
18 // Revision 1.1.1.1 2005/03/09 19:59:43 wpennington
19 // First import of OpenEMR
21 // Revision 1.2 2002/08/06 13:49:08 rufustfirefly
22 // updated rxlist class for changes in RxList.com
24 // Revision 1.1 2002/07/08 14:42:25 rufustfirefly
25 // RxList.com prescription "module" for formulary
28 if (!defined('__CLASS_RXLIST_PHP__')) {
29 define('__CLASS_RXLIST_PHP__', true);
31 class RxList {
33 function getPage ( $query ) {
34 $url = "http://www.rxlist.com/cgi/rxlist.cgi?drug=".
35 // $url = "http://129.250.146.18/cgi/rxlist.cgi?drug=".
36 urlencode($query);
38 if (!($fp = fopen($url, "r"))) {
39 // If we fail to get the page, return false
40 return false;
41 } else {
42 // Get the page
43 $buffer = 0;
44 while (!feof($fp)) {
45 $buffer .= fgets($fp, 4096);
47 fclose ($fp);
48 return $buffer;
49 } // end checking for successful open
50 } // end function RxList::getPage
52 function get_list ( $query ) {
53 $page = RxList::getPage($query);
54 $tokens = RxList::parse2tokens($page);
55 $hash = RxList::tokens2hash($tokens);
56 foreach ($hash AS $index => $data) {
57 unset($my_data);
58 foreach ($data AS $k => $v) {
59 $my_data[$k] = $v;
61 $list[trim($my_data[brand_name])." (".trim($my_data[generic_name]).")"] =
62 trim($my_data[brand_name]);
64 return $list;
65 } // end function RxList::get_list
67 function parse2tokens( $page ) {
68 $pos = 0; $token = 0; unset ($tokens);
69 $in_token = false;
70 while ($pos < strlen($page)) {
71 switch(substr($page, $pos, 1)) {
72 case "<":
73 if ($in_token) {
74 $token++;
75 $in_token = false;
77 $tokens[$token] .= substr($page,$pos,1);
78 $in_token = true;
79 break;
81 case ">":
82 $tokens[$token] .= substr($page,$pos,1);
83 $in_token = false;
84 $token++;
85 break;
87 default:
88 $tokens[$token] .= substr($page,$pos,1);
89 $in_token = false;
90 break;
92 } // end decide what to do
93 $pos++;
94 } // end looping through string
95 return $tokens;
96 } // end function RxList::parse2tokens
98 function tokens2hash ( $tokens ) {
99 $record = false; $current = 0; unset($hash); unset($all);
100 for ($pos=0; $pos<count($tokens); $pos++) {
101 if (!(strpos($tokens[$pos], "Brand Name") === false)){
102 $type = "brand_name";
103 $record = $pos;
104 $ending = "</a>";
106 if (!(strpos($tokens[$pos], "Generic Name") === false)){
107 $type = "generic_name";
108 //print "generic_name record start at $pos<BR>\n";
109 $ending = "</a>";
110 $record = $pos;
112 if (!(strpos($tokens[$pos], "Drug Class") === false)){
113 $type = "drug_class";
114 //print "drug_class record start at $pos<BR>\n";
115 $ending = "</font>";
116 $record = $pos;
118 // Handle the ending (assume when all fields are set
119 // that we're done)
120 if (isset($hash["drug_class"]) and isset($hash["brand_name"]) and isset($hash["generic_name"])) {
121 // Reset record
122 $record = false;
123 // Add hash to all
124 $all[] = $hash;
125 // Unset hash and type
126 unset($hash); $type = ""; $ending = "";
128 if ((($pos == ($record + 1)) or
129 ($pos == ($record + 2)) or
130 ($pos == ($record + 3)))
131 and ($ending != "")) {
132 //print "tokens[$pos] = ".htmlentities($tokens[$pos])."<BR>\n";
133 if ((!(strpos(strtoupper($tokens[$pos]), "</A>" ) === false)) or
134 (!(strpos(strtoupper($tokens[$pos]), "</FONT>") === false)) or
135 (!(strpos(strtoupper($tokens[$pos]), "</TD>" ) === false))) {
136 // Find where anchor is
137 $my_pos = strpos(strtoupper($tokens[$pos]), "<");
138 $hash[$type] = substr($tokens[$pos], 0, $my_pos);
139 $hash[$type] = str_replace("&amp;", "&", $hash[$type]);
140 //print "hash[$type] = ".htmlentities($hash[$type])."<BR>\n";
141 $type = ""; $ending = "";
144 } // end looping
145 return $all;
146 } // end function RxList::tokens2hash
148 } // end class RxList
150 } // end if not defined
152 // TEST CRAP HERE
153 $page = RxList::getPage("http://129.250.146.18/cgi/rxlist.cgi?drug=lipitor");
154 $tokens = RxList::parse2tokens($page);
155 $hash = RxList::tokens2hash($tokens);
156 foreach ($hash AS $k => $v) {
157 print "<UL>k = ".htmlentities($k)." \n";
158 foreach ($v AS $key => $value) {
159 print "<LI>$key = $value\n";
161 print "</UL>\n";
164 print "<FORM>\n";
165 print html_form::select_widget("test", RxList::get_list($drug));
166 print "</FORM>\n";