note 94782 added to function.preg-replace

From: Date: Tue, 24 Nov 2009 18:12:48 +0000
Subject: note 94782 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-163249@lists.php.net to get a copy of this message
Hi all, I'm pretty new to regexp pattern matching, and no rockstar with PHP, and I have an urgent need for some help to convert the CSV output from a URL to a set of table data I can use. This was much easier when I was using an XML file! When I call the URL, I am given a set of quote data formatted as such: "CLZ09.NYM","Crude Oil Dec 09","11/24/2009",76.90,"-0.56" "GCX09.CMX","Gold Nov 09","11/24/2009",1169.80,"+5.50" etc. Currently I have a script set up to read that URL and place the contents in a local file that I can mess with. Now, the symbols, names, dates, etc are not important, since they want more readable names, as indicated by the first set of <td>s in each row, and the date is going to be ignored in this table. The last two fields of each line item are what I want: the value and the change. So what I want to do is to is take that format, and eventually use it to generate this sort of structure: <tr> <td> [a more readable name that I will be inserting] </td> <td>76.90</td> <td class="down">-0.56</td> </tr> <tr> <td> [another more readable name] </td> <td>1169.80</td> <td class="up">+5.50</td> </tr> Obviously the tags will need to be echo'd in. The class for the last <td> is based on a conditional: if the number is positive, it adds the "up" class, and likewise the "down" class is if the number is negative. Also, I want to make sure all numbers are rounded or truncated to 3 decimal places (so for example, if the value is -0.0042, it would be rounded to -0.004, and if the value is -0.0048, it should round to -0.005). As I said, I am currently pulling the data I need from an XML file that is nicely formatted for my needs. It looks like this: <?xml version="1.0" encoding="UTF-8"?> <marketdata> <symbol tickersymbol="WTI" currentprice="100" change="2.5" /> <symbol tickersymbol="NY Harbor Gas" currentprice="74" change="-12" /> <symbol tickersymbol="NY Harbor Oil" currentprice="68" change="4" /> </marketdata> And my code is thus: <?php $xml = simplexml_load_file("marketdata.xml"); $incr = 0; foreach($xml->children() as $child) { echo "<tr>"; foreach($xml->symbol[$incr]->attributes() as $a => $b) { if ($a=="change" && $b>0) echo '<td class="up">',$b,'</td>'; elseif ($a=="change" && $b<0) echo '<td class="down">',$b,'</td>'; else echo '<td>',$b,'</td>'; } $incr += 1; echo "</tr>"; } ?> That works nicely, but only with my nicely formatted XML file. So I guess my first question is this: Will it be easier for me to use this data and preg_replace to simply pull out the relevant pieces and put them into my XML file, or should I simply pull the relevant pieces out and use them directly in my script? Second… can anyone help me with the preg_replace or preg_filter code I need to conniver that CSV stream into usable data either to insert into the XML file or just to use directly in a new script? Any help would be GREATLY appreciated! Thank you in advance. ---- Server IP: 69.147.83.197 Probable Submitter: 71.232.101.4 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/94782 Del: integrated -- https://master.php.net/note/delete/94782/integrated Del: useless -- https://master.php.net/note/delete/94782/useless Del: bad code -- https://master.php.net/note/delete/94782/bad+code Del: spam -- https://master.php.net/note/delete/94782/spam Del: non-english -- https://master.php.net/note/delete/94782/non-english Del: in docs -- https://master.php.net/note/delete/94782/in+docs Del: other reasons-- https://master.php.net/note/delete/94782 Reject -- https://master.php.net/note/reject/94782 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#163249) next »