= 2) && ($input[$firstIndex] == "\"") && ($input[$lastIndex] == "\"") && (strstr( substr($input,$firstIndex+1,$len-2),"\"") == null) ) array_push($ftmp,substr($input,$firstIndex + 1, $len - 2)); else array_push($ftmp,substr($input,$firstIndex, $len)); } function preserveExplode($input) { // split a string by comma, but ignore commas // between quotations marks. (This could probably be done in one // line with a regular expression.) // declare return variable $ftmp = array(); // declaration of local variables $insideQuotes = false; $firstIndex = 0; $index = 0; // Loop through characters while($index < strlen($input)) { // if comma found outside of quotes... if( ($input[$index] == ",") && (!$insideQuotes) ) { // Add sub-string to array preserveExplode_AddToArray($ftmp, $input, $firstIndex, $index - 1); // move on $firstIndex = $index + 1; $index += 1; } else { // toggle quote flag if ($input[$index] == "\"") $insideQuotes = !$insideQuotes; // move on $index++; } } // Anything left... if( $firstIndex < ($index-1) ) preserveExplode_AddToArray($ftmp, $input, $firstIndex, $index-1); // Return variable return $ftmp; } // Create the XML document $dom = new DOMDocument("1.0"); header("Content-Type: text/xml"); // create root element $root = $dom->createElement("sharePrices"); $dom->appendChild($root); try { // Replace spaces with plus in tick list $tickList = str_replace(" ","+",$_GET["tick"]); // Load the stock data from Yahoo in CSV format //urlencode( $url = "http://finance.yahoo.com/d/quotes.csv?f=sabvnpgho&s=" . $tickList; $contents = file_get_contents($url); // Explode contents by line $lines = explode("\n",$contents); // Loop through the $Lines, converting into xml elements for( $lineOffset=0; $lineOffset 0) && (ord($line[strlen($line)-1]) < 0x20) ) $line = substr($line,0,strlen($line)-1); // Explode the line $fields = preserveExplode($line); // Break on empty line if(count($fields) == 0) break; // If a single string, could be an error if(count($fields) == 1) throw new Exception($line); // create child element $sharePrice = $dom->createElement("sharePrice"); $root->appendChild($sharePrice); // create attribute node - tag $attr = $dom->createAttribute("tick"); $attrValue = $dom->createTextNode($fields[0]); $attr->appendChild($attrValue); $sharePrice->appendChild($attr); // create attribute node - $attr = $dom->createAttribute("ask"); $attrValue = $dom->createTextNode($fields[1]); $attr->appendChild($attrValue); $sharePrice->appendChild($attr); // create attribute node - $attr = $dom->createAttribute("buy"); $attrValue = $dom->createTextNode($fields[2]); $attr->appendChild($attrValue); $sharePrice->appendChild($attr); // create attribute node - $attr = $dom->createAttribute("volume"); $attrValue = $dom->createTextNode($fields[3]); $attr->appendChild($attrValue); $sharePrice->appendChild($attr); // create attribute node - name $attr = $dom->createAttribute("name"); $attrValue = $dom->createTextNode($fields[4]); $attr->appendChild($attrValue); $sharePrice->appendChild($attr); // create attribute node - previous close $attr = $dom->createAttribute("close"); $attrValue = $dom->createTextNode($fields[5]); $attr->appendChild($attrValue); $sharePrice->appendChild($attr); // create attribute node - low $attr = $dom->createAttribute("low"); $attrValue = $dom->createTextNode($fields[6]); $attr->appendChild($attrValue); $sharePrice->appendChild($attr); // create attribute node - high $attr = $dom->createAttribute("high"); $attrValue = $dom->createTextNode($fields[7]); $attr->appendChild($attrValue); $sharePrice->appendChild($attr); // create attribute node - open $attr = $dom->createAttribute("open"); $attrValue = $dom->createTextNode($fields[8]); $attr->appendChild($attrValue); $sharePrice->appendChild($attr); } } catch(Exception $e) { // create child element $errorNode = $dom->createElement("errorMessage"); $root->appendChild($errorNode); // Add error attribute to xml $attr = $dom->createAttribute("message"); $attrValue = $dom->createTextNode($e->getMessage()); $attr->appendChild($attrValue); $errorNode ->appendChild($attr); } // Return the xml document echo $dom->saveXML(); ?>