Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts

    How would I do find and replace in my text file.

    Hi,

    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:

    <salespage>http://example.com/79741/sales_page.php/</salespage>

    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:

    <salespage>http://example.com/79741/sales_page.php/</salespage>

    to

    <prod_id>79741</prod_id>

    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 ?


    Thanks


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Just wrote this up... so it's untested, but shoooould work and/or get you started in the right direction:
    PHP Code:
    // 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 ); 

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks,

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

    PHP Code:
    $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 ?



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Yeah, no issues there... might make more sense to preserve the original for the sake of archiving, etc.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •