fzx5v0 Posted November 20, 2006 Share Posted November 20, 2006 I have a text file with data I need to extract there will be about 10 pieses of data per record and about 100 records in the text file. I need to reformat it as the field names need to be different<![CDATA[ here would be the data I need ]]> I have tried yousing the explode funtion with no sucsess as i always left with this ]]> on the end can anyone point me in the right direction in what i need to do as i am not very good with php$fp = @fopen("test.txt", "rb") or die("Couldn't open file"); $data = fread($fp, filesize($fp)); while(!feof($fp)) { $data .= fgets($fp, 1024); } fclose($fp); $values = explode(" <![CDATA[", $data); Quote Link to comment Share on other sites More sharing options...
craygo Posted November 20, 2006 Share Posted November 20, 2006 Can you copy and paste a line from your text file.Ray Quote Link to comment Share on other sites More sharing options...
fzx5v0 Posted November 20, 2006 Author Share Posted November 20, 2006 here you go I also need to get out the shipping cost and best price amount- <EndNodeCategory>- <![CDATA[ Toasters ]]> </EndNodeCategory>- <Url>- <![CDATA[ http://cgi.ebay.co.uk/ws/eBayISAPI.dll?ViewItem&item=280043355467&category=20682&refid=store ]]> </Url> <Site>UK</Site> - <Description>- <![CDATA[ BREVILLE TT9 TOASTER 4 SLICE CHROME COOL WALL ]]> </Description> <EndTime>03-20-2007 16:21:33</EndTime> <Orderable>Yes</Orderable> - <Pricing> <ShippingCost>£7.99</ShippingCost> <BasePrice>£16.99</BasePrice> </Pricing> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 20, 2006 Share Posted November 20, 2006 This way works, the only problem is that it isn't capturing data between [b]<Description><![CDATA[[/b] and [b]]]></Description>[/b], and I don't know why[code]<?php$fp = @fopen("test.txt", "rb") or die("Couldn't open file");//$data = fread($fp, filesize($fp));while(!feof($fp)){$data .= fgets($fp, 1024);}fclose($fp);//$values = explode(" <![CDATA[", $data);$search = array( "<Site>(.+?)</Site>", "<Description><![CDATA[ (.+?) ]]></Description>", "<EndTime>(.+?)</EndTime>", "<Orderable>Yes</Orderable>", "<ShippingCost>(.+?)</ShippingCost>", "<BasePrice>(.+?)</BasePrice>" );$new = array( "$1", "$1", "$1", "$1", "$1", "$1" );str_replace($search, $new, $data);echo $data;?>[/code] Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 20, 2006 Share Posted November 20, 2006 OK... I found something, but I had to modify your text file if that is ok....Here is my version of the text file:[code]- <EndNodeCategory>Toasters</EndNodeCategory>- <Url>http://cgi.ebay.co.uk/ws/eBayISAPI.dll?ViewItem&item=280043355467&category=20682&refid=store</Url> <Site>UK</Site>- <Description>BREVILLE TT9 TOASTER 4 SLICE CHROME COOL WALL</Description> <EndTime>03-20-2007 16:21:33</EndTime> <Orderable>Yes</Orderable>- <Pricing> <ShippingCost>£7.99</ShippingCost> <BasePrice>£16.99</BasePrice> </Pricing>[/code]and here is my PHP:[code]<?php$handle = fopen("test.txt", "r"); while (!feof($handle)){ $data .= fgets($handle); }function data_replace($strInput){$search = array( '/\<EndNodeCategory\>(.+?)\<\/EndNodeCategory\>/i', '/\<Site\>(.+?)\<\/Site\>/i', '/\<url\>(.+?)\<\/url\>/i', '/\<Description\>(.+?)\<\/Description\>/i', '/\<EndTime\>(.+?)\<\/EndTime\>/i', '/\<Orderable\>(.+?)\<\/Orderable\>/i', '/\<ShippingCost\>(.+?)\<\/ShippingCost\>/i', '/\<BasePrice\>(.+?)\<\/BasePrice\>/i' );$new = array( '<b>End Node Category:</b> $1<br>', '<b>Site:</b> $1<br>', '<b>URL:</b> $1<br>', '<b>Description:</b> $1<br>', '<b>End Time:</b> $1<br>', '<b>Orderable:</b> $1<br>', '<b>Shipping Cost:</b> $1<br>', '<b>Base Price:</b> $1<br>' );$data = preg_replace($search, $new, $strInput);return strip_tags(str_replace('-','', trim($data)), '<br> <b>');}echo data_replace($data);?>[/code] Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted November 21, 2006 Share Posted November 21, 2006 Your text file looks very much like xml. If it is, just use an xml library to parse it into an array...I recommend minixml. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.