This project just got dumped on me.
I have a text file that has several lines in it. Like this:

<cat:8850>
<begad:1279434><logo:><togurl:y><togemail:y><bkgrdtog:n><addr:ATTN:CHARLIE><zip:50613>'05 Pontiac Grand Am SE, 35,200 mi., $9,600 or best reasonable offer.  Contact Charlie at 319-266-XXX<endad>
<cat:8850>
<begad:1278968><logo:><togurl:y><togemail:y><bkgrdtog:n><addr:34304 160TH ST.><zip:50613>'07 GRAND Prix, red, GM certified, 35,000 mi. Leather, sunroof, remote start.     Laid off.  $14,000    Must sell. 319-215-XXX<endad>

I need to get the top line of these entries with the rest of the string and repeat them. Like this:

<cat:8850><begad:1279434><logo:><togurl:y><togemail:y><bkgrdtog:n><addr:ATTN:CHARLIE><zip:50613>'05 Pontiac Grand Am SE, 35,200 mi., $9,600 or best reasonable offer.  Contact Charlie at 319-266-XXXX<endad>
<cat:8850><begad:1278968><logo:><togurl:y><togemail:y><bkgrdtog:n><addr:34304 160TH ST.><zip:50613>'07 GRAND Prix, red, GM certified, 35,000 mi. Leather, sunroof, remote start.     Laid off.  $14,000    Must sell. 319-215-XXXX<endad>

I can get them into one continuous string, but that's not really working for me. Here's what I have so far:

$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'rb') or die("can't open file");

$numberd = 0;
while (!feof($ourFileHandle) ) {
	$line_of_text = fgets($ourFileHandle, 4062);
	$new_string = preg_replace("/[^a-zA-Z0-9\s]/", "", $line_of_text);
	$parts = explode('\n', $new_string);

	$adcopy = $parts[0] . $parts[1];
	$numberd++;
	
echo $adcopy . "|";

}
fclose($ourFileHandle);

Any help would be super. Thank you.

I'm not exactly sure what you want to do, the two examples you posted are the same except for a newline after the <cat #> tag

That's right, I need that <cat #> tag dropped down so that it is in the same string as the rest of the stuff. The text file I'm getting has like 50 of these entries and I need them reorganized so that I can explode them and move their parts around. That's what I was thinking anyway.

change all of the file read logic to just be $file = file('filename') then use

$file = preg_replace('/(<cat:\d+?>).+?(<begad:\d+?>)/sm', '\1\2', $file);

Then $file is your entire file with the fixed strings, each data item on its own line.

Whoops, forgot that file() returns an array so here's how it should look.

$contents = implode("\n", file('filename'));
$contents = preg_replace('/(<cat:\d+?>).+?(<begad:\d+?>)/sm', '\1\2', $contents);
$contents = explode("\n", $contents);

foreach($contents as $line => $stuff) {
 // each $stuff is one entry
}

That actually ripped out everything that was in between <> tags, and I need to hang on to some of that. I had a similar result messing around with it yesterday. That's ok though. I'm trying a slightly different method to get this feed to look like I need it to.

I really appreciate you getting back with me and so quickly too. Enjoy the weekend.

This article has been dead for over six months. Start a new discussion instead.