You are here:  » Search and Replace Words During Import of Files


Search and Replace Words During Import of Files

Submitted by ItsDavid on Sat, 2016-04-23 06:41 in

Hi, I am working on trying to setup a search and replace words or terms system within my price tapestry so let me try to explain my intentions in an explainable way.

What i want to do is add my feed URL which creates my XML files in my feeds folder. This works great.

Now what i would like to do is add either a "Word or Term Remover" file where i add words and terms to remove or an added area within the admin panel that i can add a comma separated array of words or terms to be removed hit save and have price tapestry scan these XML files during creation/import or after and remove the words and terms and then just overwrite and save the file if it is a process that has to be done after creation of the feed files. If the word and term remover is possible during creation of these XML files than there is no need for extra processing of the XML files after creation.

I found this thread http://www.pricetapestry.com/node/505 however this seems to apply to database items. I have XML files in the feeds folder only nothing in the database.

Now i am not sure if elements within the XML file have to programmatically be targeted such as

Item > Name

Item > Description

Item > Short Description

ETC...

or if a global str_replace can be used to search the XML file in it's entirety and remove all instances of words or terms i specify.

Just to clarify in case it matters there will be hundreds or words and terms i need to remove due to many many different feeds i work with.

Thank You

Submitted by support on Fri, 2016-04-29 12:04

Hi David,

As you mentioned the filtering process operates on a data as it is imported - editing within a stream (e.g. as downloading) is actually very complex, however it would be no problem to create a new filter - Search and Replace CSV that takes the words / phrases to swap from a .csv file saved in the /feeds/ folder e.g.

/feeds/searchreplace.csv

...containing, for example:

Word1,Replace1
Word2,Replace2
Search Phrase 1,Replace Phrase 1
Search Phrase 2,Replace Phrase 2

With the .csv file of your search words / phrase and replacements in place, create the new filter by adding the following code to includes/filter.php:

  /*************************************************/
  /* searchReplaceCSV */
  /*************************************************/
  $filter_names["searchReplaceCSV"] = "Search and Replace CSV";
  function filter_searchReplaceCSVConfigure($filter_data)
  {
    print "<p>There are no additional configuration parameters for this filter.</p>";
  }
  function filter_searchReplaceCSVValidate($filter_data)
  {
  }
  function filter_searchReplaceCSVExec($filter_data,$text)
  {
    global $config_feedDirectory;
    global $filter_searchReplaceS;
    global $filter_searchReplaceR;
    if (!isset($filter_searchReplaceS))
    {
      $filter_searchReplaceS = array();
      $filter_searchReplaceR = array();
      $fp = fopen($config_feedDirectory."searchreplace.csv","r");
      while($line = fgets($fp))
      {
        $parts = explode(",",$line);
        $filter_searchReplaceS[] = $parts[0];
        $filter_searchReplaceR[] = $parts[1];
      }
      fclose($fp);
    }
    return str_replace($filter_searchReplaceS,$filter_searchReplaceR,$text);
  }

Then simply attach a new Search and Replace CSV filter as a Global Filter to the fields you wish to modify...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ItsDavid on Wed, 2016-05-11 03:52

Hi David, I might be doing something wrong or maybe missing a step but this does not work to search and replace words and terms in my XML files downloaded to my "feeds" folder.

Let me explain what i did to see if i missed something.

I created my searchreplace.csv file with all terms and words in the following format

old first term,new first term
old second term,new second term
old third term,new third term

etc...

I then added searchreplace.csv to my "feeds" folder.

I added the code above to the bottom of includes/filter.php:

Now i went to the admin panel and added two new global filters

Search and Replace CSV ↑↓Configure Delete Product Name

Search and Replace CSV ↑↓Configure Delete Description

After adding these global filters i went to the automation tool and added my URL to download my XML file to my feeds folder "uncompressed" that worked.

i opened the XML file to inspect to see if anything was changed and no changes took place.

Is there something i missed?

Just to note i am not importing these XML feed files into the database.

Also some of the terms or words i want to remove have (,) commas in them so would it be better to use a (|) pipe separated CSV file? and just change the

$parts = explode(",",$line);

to

$parts = explode("|",$line);

Another question i have is that some of the words and terms present i want to remove completely so would the following work to remove the terms or words?

Remove First Word,
Remove Second Word,
Remove Third Word,

or if using pipe separated.....

Remove First Word|
Remove Second Word|
Remove Third Word|

Thank you for all your help with this.

Best Regards

David

Submitted by support on Wed, 2016-05-11 08:35

Hi David,

Filters operate against data as it is imported, not as feeds are downloaded which is incredibly complicated to do (editing a stream or even making search and replace changes to very large files once downloaded), so it looks like you've done everything as suggested but the changes would affect your imported, not simply downloaded data.

If your phrases involve commas then yes - using pipe is fine, so exactly as you have identified in place of;

         $parts = explode(",",$line);

...use:

         $parts = explode("|",$line);

Just to help point you in a possible direction if physically editing feeds is your objective, if you are working in a Linux environment have a look at sed (a stream editor) but as this is right outside the scope of Price Tapestry functionality I'm afraid I wouldn't be able to assist with implementation at all but it might be something worth investigating!

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ItsDavid on Wed, 2016-05-11 19:19

Thanks for the feedback David, I took a look at sed and it appears it is above my programming education.

I was hoping this could be tackled in an easy way but i guess that's not the case.

I thought i seen somewhere that curl had the ability to open files modify them and save them but maybe i am mistaken.

Can i ask......

Is it possible to import my feed files into the database so that the searchreplace.csv akes effect and then return the information in the price tapestry database in xml format using a url call for say

Merchants

Products

Voucher Codes (Coupon Codes)

Categories?

Is this what the sitemap feature does is return an xml version for search engines?

Price Tapestry is perfect for what i need except for the little search and replace hurdle i am up against.

Best Regards

Submitted by support on Thu, 2016-05-12 10:49

Hello David,

There are numerous threads on exporting data from Price Tapestry - there may be something very close to what you are looking to try and do. Use the Site Search form at

http://www.pricetapestry.com/forum/

And search on

export
export csv
export xml

If there is something very close but not quite what you're looking for but you're not sure how to convert let me know and I'll try and point you in the right direction...

Cheers,
David.
--
PriceTapestry.com