Results 1 to 10 of 10
  1. #1
    Newbie
    Join Date
    January 15th, 2008
    Location
    Portage, MI
    Posts
    39
    Smile Preg_replace not working
    I have been pulling what little hair I have left trying to get my preg_replace code to work with my import code.

    I would like to hardcode it into my code, can that be done or do I need to run it as a seperate script?

    Are the changes supposed to show up in the MySQL table? I am assuming so.

    Here is the code:

    <?php

    $iLDescription = trim($row["LDescription"]);
    $IProductName = trim($row["ProductName"]);
    $iBrand = trim($row["Brand"]);

    $search = array('/\(/', '/\)/', '/Fall/', '/Spring/', '/2006/', '/2007/', '/2008/', '/2005/');
    $replace = array ('', '', '', '', '', '', '', '');

    $iDescription=preg_replace($search, $replace, $iLDescription);
    $iProductName=preg_replace($search, $replace, $iProductName);

    $search = array('/stream/');
    $replace = array('/creek/');

    $iBrand=preg_replace($search, $replace, $iBrand);

    echo "Search and replace complete";
    ?>
    When I run it separate the php tag are there as seen, when I put it at the end of the import code I take the php tags out.

    I have tried it at the end of the code and before the import.

    Either way it will give me the echo code but nothing changes in the table.


    Save My Hair!!

  2. #2
    Comfortably Numb John Powell's Avatar
    Join Date
    October 17th, 2005
    Location
    Bayou Country, LA
    Posts
    3,432
    Are the changes supposed to show up in the MySQL table? I am assuming so.
    You would have to have a QUERY down stream with an INSERT or an UPDATE. There is not enough code there for us to tell.


  3. #3
    Newbie
    Join Date
    January 15th, 2008
    Location
    Portage, MI
    Posts
    39
    Here is the code again:

    <?php
    require_once ('../connect_purecountry_local.php');

    mysql_query("UPDATE TABLE moosejaw");

    $Description = trim($row["LDescription"]);
    $Title = trim($row["ProductName"]);
    $Brand = trim($row["Brand"]);

    $searchDT = array('/\(/', '/\)/', '/Fall/', '/Spring/', '/2006/', '/2007/', '/2008/', '/2005/');
    $replace = array ('', '', '', '', '', '', '', '');

    $Description=preg_replace($searchDT, $replace, $Description);
    $Title=preg_replace($searchDT, $replace, $Title);

    $searchB = array('/Arcteryx/');
    $replace = array('/Arc\'teryx/');

    $Brand=preg_replace($searchB, $replace, $Brand);

    echo "Search and replace complete";

    ?>

  4. #4
    Newbie
    Join Date
    January 15th, 2008
    Location
    Portage, MI
    Posts
    39
    Sorry, copy and pasted wrong code:

    <?php

    set_time_limit(500);

    require_once ('../connect_purecountry_local.php');



    // File can be anywhere on the Internet
    $FeedFile = 'http://xxxxxxxxxxxxxxxxxxxxxx.txt';

    mysql_query("drop table xxxxxxxxx_temp");
    mysql_query("CREATE TABLE xxxxxxxx_temp (
    ID INT NOT NULL AUTO_INCREMENT,
    Sku VARCHAR(50) NOT NULL,
    Brand VARCHAR(150) NOT NULL default '',
    ProductName VARCHAR(60) NOT NULL default'',
    LDescription TEXT NOT NULL default'',
    SDescription TEXT NOT NULL,
    Category TINYTEXT NOT NULL,
    SCategory TINYTEXT NOT NULL,
    ProdGroup TINYTEXT NOT NULL,
    ThumbURL BLOB NOT NULL default'',
    ImageURL BLOB NOT NULL default'',
    BuyLinkURL BLOB NOT NULL default'',
    Keywords TEXT NOT NULL,
    RetailPrice VARCHAR(7) NOT NULL default '0.00',
    SalePrice VARCHAR(7) NOT NULL default '0.00',
    TrackingURL BLOB NOT NULL default'',
    PRIMARY KEY (ID))") or die(mysql_error());

    $feed = fopen($FeedFile, 'r');

    $rowNum = 0;
    $recCount = 0;


    while($data = fgetcsv($feed, 3000, "\t")){
    if($rowNum > 0){
    $iSku = addslashes($data[0]);
    $iBrand = addslashes($data[1]);
    $iProductName = addslashes($data[2]);
    $iLDescription = addslashes($data[3]);
    $iSDescription = addslashes($data[4]);
    $iCategory = addslashes($data[5]);
    $iSCategory = addslashes($data[6]);
    $iProdGroup = addslashes($data[7]);
    $iThumbURL = addslashes($data[8]);
    $iImageURL = addslashes($data[9]);
    $iBuyLinkURL = addslashes($data[10]);
    $iKeywords = addslashes($data[11]);
    $iRetailPrice = addslashes($data[12]);
    $iSalePrice = addslashes($data[13]);
    $iTrackingURL = addslashes($data[14]);

    $sql = mysql_query("insert into xxxxxx_temp(Sku, Brand, ProductName, LDescription, SDescription, Category, SCategory, ProdGroup, ThumbURL, ImageURL, BuyLinkURL, Keywords, RetailPrice, SalePrice, TrackingURL)
    values
    ('$iSku', '$iBrand', '$iProductName','$iLDescription', '$iSDescription', '$iCategory', '$iSCategory', '$iProdGroup', '$iThumbURL', '$iImageURL', '$iBuyLinkURL', '$iKeywords', '$iRetailPrice', '$iSalePrice', '$iTrackingURL')") or die(mysql_error());
    $recCount++;
    }
    $rowNum++;
    }

    fclose ($feed);
    echo "Avantlink xxxxx Import Completed Successfully<br>";





    mysql_query("DROP TABLE xxxxxxxxx");
    mysql_query("ALTER TABLE xxxxx_temp RENAME xxxxx") or die(mysql_error());


    $LDescription = trim($row["LDescription"]);
    $ProductName = trim($row["ProductName"]);
    $Brand = trim($row["Brand"]);

    $search = array('/\(/', '/\)/', '/Fall/', '/Spring/', '/2006/', '/2007/', '/2008/', '/2005/');
    $replace = array ('', '', '', '', '', '', '', '');

    $Description=preg_replace($search, $replace, $LDescription);
    $ProductName=preg_replace($search, $replace, $ProductName);

    $search = array('/Arcteryx/');
    $replace = array('/Arc\'teryx/');

    $Brand=preg_replace($search, $replace, $Brand);

    echo "Search and replace complete";

    ?>

  5. #5
    ABW Veteran Mr. Sal's Avatar
    Join Date
    January 18th, 2005
    Posts
    6,795
    Quote Originally Posted by country
    I have been pulling what little hair I have left trying to get my preg_replace code to work with my import code.

    I would like to hardcode it into my code, can that be done or do I need to run it as a seperate script?

    Are the changes supposed to show up in the MySQL table? I am assuming so.

    Here is the code:


    When I run it separate the php tag are there as seen, when I put it at the end of the import code I take the php tags out.

    I have tried it at the end of the code and before the import.

    Either way it will give me the echo code but nothing changes in the table.


    Save My Hair!!
    Awhile ago I had a merchant with some similar junk on the datafeed, and the first time I found a solution to remove all that junk, I did it just on the product description pages with a code similar to the one you posted, but that only fixed the problem on the product description pages.

    Much later I learned how to do it on the datafeed upload script so it will delete all that junk from the feed before I used the feed.

    You can try something like this:

    $recCount++;
    }
    $rowNum++;
    }
    mysql_query("update xxxxxx_temp set Description = replace(Description,'2005','')") or die(mysql_error());
    mysql_query("update xxxxxx_temp set Description = replace(Description,'2006','')") or die(mysql_error());
    mysql_query("update xxxxxx_temp set Description = replace(Description,'2007','')") or die(mysql_error());
    mysql_query("update xxxxxx_temp set Description = replace(Description,'2008','')") or die(mysql_error());
    mysql_query("update xxxxxx_temp set Description = replace(Description,'Fall','')") or die(mysql_error());
    mysql_query("update xxxxxx_temp set Description = replace(Description,'Spring','')") or die(mysql_error());
    mysql_query("DROP TABLE xxxxxxxxx");
    mysql_query("ALTER TABLE xxxxx_temp RENAME xxxxx") or die(mysql_error());
    Notice that I put my CODE between your:

    ------------------------
    $rowNum++;
    }


    and your:

    mysql_query("DROP TABLE xxxxxxxxx");
    ------------------------


    There may be a better way, but that code works for me.



  6. #6
    Newbie
    Join Date
    January 15th, 2008
    Location
    Portage, MI
    Posts
    39
    Thanks Mr. Sal

    That worked, although I was hoping to get away from writing so much code. I will use this until I find something faster.

    Now its on to a writing sprint until I hit another hurdle.

    Hair saved for now!!

  7. #7
    ABW Veteran Mr. Sal's Avatar
    Join Date
    January 18th, 2005
    Posts
    6,795
    Quote Originally Posted by country
    Thanks Mr. Sal

    That worked,
    You're welcome country.
    although I was hoping to get away from writing so much code. I will use this until I find something faster.
    Tell me about it!

    I know that there must be a better way, but for now I am just dealing with one merchant weird datafeed at the time.

    Like I said above, the first time I encounter a datafeed with all that junk in there, I learned how to fix the code on the product pages only, and I hated all that similar crap code: [$search = array('/\(/', '/\)/', '/Fall/', '/Spring/', '/2006/', '/2007/', '/2008/', '/2005/');
    $replace = array ('', '', '', '', '', '', '', '');
    ] in there.

    But later I learned how to do it on the import script, one line at the time.

    I know it sucks to have to write all that extra code just for some merchants datafeed, but for now I am ok with that code, because I made one large test junkcode.php fix page, on where I have most of the crap code I would need to do a search and replace, on the next junk merchant datafeed that I may want to work with.

    Just like code I give you, I have template of:

    mysql_query("update $thistable set $fieldname = replace($fieldname,'$junk','$keyword')") or die(mysql_error());
    So even if I have to change 20 junk words (20 new lines of code), I have no problem with it. (for now)

    Hair saved for now!!
    Btw, save some of your hair, but remember that even some barbers need to make a living too.

  8. #8
    Full Member
    Join Date
    October 22nd, 2006
    Posts
    200
    Hi

    Wouldn't it be easier if you moved this block of code:

    PHP Code:
    $search = array('/\(/''/\)/''/Fall/''/Spring/''/2006/''/2007/''/2008/''/2005/');
    $replace = array ('''''''''''''''');

    $iLDescription=preg_replace($search$replace$iLDescription);
    $iProductName=preg_replace($search$replace$iProductName);

    $search = array('/stream/');
    $replace = array('/creek/');

    $iBrand=preg_replace($search$replace$iBrand); 
    just after

    PHP Code:
    $iTrackingURL addslashes($data[14]); 
    and before your insert into statement?

  9. #9
    Newbie
    Join Date
    January 15th, 2008
    Location
    Portage, MI
    Posts
    39
    Morning Donk,

    I tried that and either I don't have my code right or it just plain doesn't work. It makes no changes at all. Always open for suggestions.

    I had thought that was where it should go to. Tried it there and at the end of the code, don't know where else to try it. Can't do it before the import.

  10. #10
    Newbie
    Join Date
    January 15th, 2008
    Location
    Portage, MI
    Posts
    39
    Forgot, just got up 5 min ago

    Morning Mr. Sal,

    I like your idea of a template for the code, makes it a copy and paste step and cuts down on time. Thanks for the tip.

    Someday I'll find an answer to my predicament and then we'll know.

    By the way, I'm more worried about paying a physcyatrist for now, than about my hair. hehe

  11. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. Is CJ working?
    By Aunt Lily in forum Commission Junction - CJ
    Replies: 14
    Last Post: December 6th, 2007, 06:38 PM
  2. IS CJ working ??
    By parpat in forum Commission Junction - CJ
    Replies: 5
    Last Post: March 15th, 2004, 06:46 PM

Posting Permissions

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