Jump to content

exract pices of text from a text file


fzx5v0

Recommended Posts

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);
Link to comment
https://forums.phpfreaks.com/topic/27894-exract-pices-of-text-from-a-text-file/
Share on other sites

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>
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]
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.