Page 1 of 1

To extract particular data`s from text file.

Posted: Fri Oct 03, 2008 6:16 am
by venuram
Hi,

I am new to this forum.Please any one tell "how to extract the particular data`s from text file" and parse into CSV file.

For ex:

Profile ID: 632315 matches...
Title: Social network webmaster-using kickapps
Project ID: 446837

Category: Website Design / Website Marketing
Description:
I have a new social network website using the http://www.kickapps.com platform. I need someone that can use their skills to make our site standout and be attractive. This project is for initial design. Potential for long term employment.
Besides their main website check out http://www.kickdeveloper.com/ for additional information.

---------------------------------------------------


---------------------------------------------------
Profile ID: 632315 matches...
Title: two niche websites
Project ID: 446839

Category: Website Design / Website Marketing
Description:
Need help with design for two niche websites to enhance income potential.
One - credit score website
second one is a health/medical website.

Project is for one time makeover of site. Potential for ongoing employment.

---------------------------------------------------
From the above data`s i have to extract only the Title: and Project ID:.

That is output must be like this:

Social network webmaster-using kickapps
446837


Please anyone help me.

Thanks,
Venu

Re: To extract particular data`s from text file.

Posted: Fri Oct 03, 2008 8:55 am
by Kadanis
The only option I can think of is to use a regular expression along with the preg_match_all function. That will generate an array with all the titles and project id's which you can then parse into your CSV file

For example:

Code: Select all

 
$inputData = 'text from the file.....';
 
$regexProjectID = '@Title: (.*)\nProject ID: (\d{6})@m';
 
preg_match_all($regexProjectID, $inputData, $output);
 
$projects = array_combine($output[2], $output[1]);
 
//create the csv formatted output
$csv = "project_id,poject_title\n";
foreach ($projects as $id => $title) {
    $csv .= $id . ',' . $title . "\n";
}
echo $csv;
 
This piece of code will extract the Title and Project ID from any number of entries in the input data file and put them into an array with the Project ID as the key and the Title as the value. The final loop creates csv formatted data in the variable $csv which could then be output to the browser with the correct headers for download, or written to a file

I only put this together as an example though so it assumes that the Title and Project ID will always be in the format that are in your example, i.e One separate rows, with Title first followed by ID. ID will always be 6 digits long.

Obviously you can edit the regular expression to fit your needs more precisely.

Hope this helps