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
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>
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.