View Full Version : How would I do find and replace in my text file.

02-21-2010, 07:53 PM

I am using a text file that contains tags.

I download it everyday and want to process it with my php script.

It is a very annoying file as it does not contain
product codes except when referencing the sales page.

For example:


Now I could put the sales page into my tabe and index it ...
... but that would waste a lot of space,
as the "http://example.com/" and "sales_page.php" are always the same,
only that number in the middle changes.

I just need to store the number in a column "prod_id"

What is the best way to go through this text file
and convert:




Although this is an xml file, I want to make this change to the file before I use simplexml_load_file($file);

When the change is done ı will open it up with simplexml_load_file($file);

As there are several thousand records should I be using a regular experssion to do this or is there quicker method ?



02-21-2010, 08:42 PM
Just wrote this up... so it's untested, but shoooould work and/or get you started in the right direction:

// Grab the XML source
$source = file_get_contents( $xml_file );

// Locate all <salespage> data, along with the corresponding ID
preg_match_all( '/\<salespage\>http\:\/\/example\.com\/(\d+)\/sales_page\.php\/\<\/salespage\>/', $source, $matches );

// Replace all found <salespage> instances with the <prod_id> tags surrounding the ID
foreach ( $matches[0] as $i => $salespage_tag )
$source = str_replace( $salespage_tag, '<prod_id>' . $matches[1][$i] . '</prod_id>', $source );

// Write it back the the original file
file_put_contents( $xml_file, $source );

02-21-2010, 10:04 PM

Would it be "safer" if I wrote it to a new file ?

$xml_out = "new_xml.xml";

// Write it back the the original file
file_put_contents( $xml_out, $source );

Or does that mess up the script ?


02-21-2010, 10:23 PM
Yeah, no issues there... might make more sense to preserve the original for the sake of archiving, etc.