marty.net 1001 Postcards Love Love Love Meep Meep ZenZodiac DailyQuotation

YOU ARE HERE:
Advanced String Replacement using cpdig

HOME

CPSHOP|

|

|

EMAIL MARTY
  marty.net message board
  « Advanced String Replacement using cpdig »
Welcome, Guest. Please Login or Register.
12/13/17 at 16:57:22
Home Help Search Login Register
   marty.net message board
   About cpshop
   Tutorials
   Advanced String Replacement using cpdig
(Moderator: Marty)
Pages: 1 2 » Reply Notify of replies Send the topic Print 
   Author  Topic: Advanced String Replacement using cpdig  (Read 8378 times)
Hex Angel - Patrick
Gold Dragon
****

Posts: 500



Our nature lies in movement; complete calm is death.
   

WWW
Advanced String Replacement using cpdig
« on: 03/27/07 at 15:07:08 »
Reply with quote Modify message

Hey there all,

Here's a tutorial for doing advanced string replacement, including regular expressions, on cpshop pages. While the cpshop image file is useful for performing simple search and replace functions, there are some thorny strings that defy replacement (eg "[beta]" and "Available Printing Method(s)") and there is no support for wild cards. The methods outlined in the tutorial will allow you to replace any string with any other string and provides powerful wildcard options through regular expressions.

There are some advanced topics and novices will want to learn a few things about the PERL search and replace function and regular expressions (there are links to online references for each in the tutorial.) However, I've taken pains to describe every necessary step in succinct detail so most folks should be able to navigate it.

The tutorial's a bit big for this forum so I've created a page for it here.  If you have any questions, or experience any difficulties, please post them to this forum topic or send me a PM.

Cheers,

Patrick.
« Last Edit: 03/27/07 at 16:32:11 by Hex Angel - Patrick » Logged

CPSHOP
www.PeaceAndLoveShop.com Namaste
BLOG
www.TheNomadicSpirit.com Inspired by travel
www.GodBeautyPerfectionLove.com Inspired by spirit
www.MyMuseMoviesMusicAndBooks.com Inspired by the arts
Bradley W. Schenck
Gold Dragon
****

Posts: 242



Go Scots!
   

WWW
Re: Advanced String Replacement using cpdig
« Reply #1 on: 03/28/07 at 10:45:38 »
Reply with quote Modify message

This looks interesting!  I'll probably give it a try, hoping that there won't be strange interactions with anything else I'm doing that could lead to the destruction of modern civilization.

One thing, though - I'm pretty sure I've been able to get rid of that "Tell a friend!" link via the images file.
Logged
Hex Angel - Patrick
Gold Dragon
****

Posts: 500



Our nature lies in movement; complete calm is death.
   

WWW
Re: Advanced String Replacement using cpdig
« Reply #2 on: 03/28/07 at 17:47:42 »
Reply with quote Modify message

<grin>  I've been using this code since January and modern civilization hasn't crumbl...  Well, ummm, I'm pretty sure the present collapse doesn't have anything to do with my code.

And, oops, put that line in the wrong block.

[EDIT: Ahh, yes, Bradley, the image file can take out the "tell a friend" text string. However, my version removes the launchHelp link as well -- the image file can't (because it contains parenthesese '(' and ')').]

Cheers!

p.
« Last Edit: 03/28/07 at 19:19:17 by Hex Angel - Patrick » Logged

CPSHOP
www.PeaceAndLoveShop.com Namaste
BLOG
www.TheNomadicSpirit.com Inspired by travel
www.GodBeautyPerfectionLove.com Inspired by spirit
www.MyMuseMoviesMusicAndBooks.com Inspired by the arts
Bradley W. Schenck
Gold Dragon
****

Posts: 242



Go Scots!
   

WWW
Re: Advanced String Replacement using cpdig
« Reply #3 on: 10/31/09 at 07:04:40 »
Reply with quote Modify message

I have a feeling that the answer to this is boneheadedly simple (because, you know, that's me) but I'm having a problem using cpdig to replace a section of text with a cpshop tag or hook.

I'm trying to replace the "Additional Product Info" area with a hook. For simplicity's sake, though, let's use a tag. The result is the same.

I use this line:


Code:
$html =~ s/CafePress Product Description -->.*?<!-- \/CP Product Description/--><[productname]><!--/sgi;


and what I get is this:


Code:
-->&lt;[productname]&gt;<!--


...which just results in the text "<[productname]>" rather than the tag I wanted.  I've tried escaping the carat brackets, which are the characters that are rebelling, but that's not it.  Help?

Edit: I've realized that the first and last brackets (closing and beginning comments) are coming through correctly. It's the two that surround the tag or hook that don't.
« Last Edit: 10/31/09 at 07:11:51 by Bradley W. Schenck » Logged
Hex Angel - Patrick
Gold Dragon
****

Posts: 500



Our nature lies in movement; complete calm is death.
   

WWW
Re: Advanced String Replacement using cpdig
« Reply #4 on: 10/31/09 at 08:19:58 »
Reply with quote Modify message

Hmmmm....

that's really odd, and I don't have a quick answer. It's been so long since I've had to dig into that code!

I'll see if I can repro it later...

One observation on your code:

Take the 'g' out of 'sgi' in your code for this one. The 'g' means "search and replace all occurrences".  If you don't specify the the 'g' then the search and replace stops at the first occurence. Since there's only ever one instance of this search string in a page, you don't need to keep searching for another.
Logged

CPSHOP
www.PeaceAndLoveShop.com Namaste
BLOG
www.TheNomadicSpirit.com Inspired by travel
www.GodBeautyPerfectionLove.com Inspired by spirit
www.MyMuseMoviesMusicAndBooks.com Inspired by the arts
Bradley W. Schenck
Gold Dragon
****

Posts: 242



Go Scots!
   

WWW
Re: Advanced String Replacement using cpdig
« Reply #5 on: 11/01/09 at 07:01:15 »
Reply with quote Modify message

Edit: part of this morning's exercise was pre-coffee operator error.

I'm where I was yesterday - though I am able to get rid of the framing comments. I still don't have a working CPShop tag, as above.
« Last Edit: 11/01/09 at 07:45:06 by Bradley W. Schenck » Logged
Hex Angel - Patrick
Gold Dragon
****

Posts: 500



Our nature lies in movement; complete calm is death.
   

WWW
Re: Advanced String Replacement using cpdig
« Reply #6 on: 11/01/09 at 11:59:53 »
Reply with quote Modify message

Hey Bradley,

cpshop has already done the hook and tag processing by the time cpdig gets to play with the page, so, unfortunately you can't use cpdig to add hooks or tags ... at least not that way.

There is a workaround for this kind of thing...

Say you put something like this in your template -- anywhere, it doesn't matter:

<!-- REPLACEMENT <[productname]> REPLACEMENT -->

Then add something like the following in your cpdig... (untested!).


Code:
$replace = $html;
$replace =~ s/^.*?<!-- REPLACEMENT (.*?) REPLACEMENT -->.*?$/$1/sgi;
$html =~ s/CafePress Product Description -->.*?<!-- \/CP Product Description/-->$replace<!-- CP Product Description/sgi;


So, what you're doing is storing a value -- it could be a tag or hook -- in your template to be retrieved by cpdig and replaced wherever you want it.

hth,

Patrick.


« Last Edit: 11/01/09 at 17:36:06 by Hex Angel - Patrick » Logged

CPSHOP
www.PeaceAndLoveShop.com Namaste
BLOG
www.TheNomadicSpirit.com Inspired by travel
www.GodBeautyPerfectionLove.com Inspired by spirit
www.MyMuseMoviesMusicAndBooks.com Inspired by the arts
Bradley W. Schenck
Gold Dragon
****

Posts: 242



Go Scots!
   

WWW
Re: Advanced String Replacement using cpdig
« Reply #7 on: 11/01/09 at 13:46:28 »
Reply with quote Modify message

Well, I get errors in the second and third line.  I'm not clear how my hook could have been processed anyway, since we've seen it's too late. (Thanks!)

I may just throw in the towel on this one.

It would have been nice to do - I'd hoped to replace the "More Product Information" section of the product page, to correct some CP errors and to help me avoid a duplicate content situation - but it looks sort of like What Man Was Not Meant to Wot Of, doesn't it?
« Last Edit: 11/01/09 at 13:47:21 by Bradley W. Schenck » Logged
Hex Angel - Patrick
Gold Dragon
****

Posts: 500



Our nature lies in movement; complete calm is death.
   

WWW
Re: Advanced String Replacement using cpdig
« Reply #8 on: 11/01/09 at 13:53:16 »
Reply with quote Modify message

<grin> Ahhh, hooks. It's a bit to wrap your head around, eh?

Don't give up on it just yet.

The hook gets processed because it's in the template file itself.

All you're trying to do with cpdig is dig it back out again after cpshop has filled in the data for you.  I'll make it work...be right back
Logged

CPSHOP
www.PeaceAndLoveShop.com Namaste
BLOG
www.TheNomadicSpirit.com Inspired by travel
www.GodBeautyPerfectionLove.com Inspired by spirit
www.MyMuseMoviesMusicAndBooks.com Inspired by the arts
Bradley W. Schenck
Gold Dragon
****

Posts: 242



Go Scots!
   

WWW
Re: Advanced String Replacement using cpdig
« Reply #9 on: 11/01/09 at 13:55:11 »
Reply with quote Modify message

...Though now I wonder if I could just plant a div with an ID in there, and rewrite its content on load with Javascript.  Yikes.

oops. Simultaneous posts .
« Last Edit: 11/01/09 at 13:56:01 by Bradley W. Schenck » Logged
Bradley W. Schenck
Gold Dragon
****

Posts: 242



Go Scots!
   

WWW
Re: Advanced String Replacement using cpdig
« Reply #10 on: 11/01/09 at 15:08:46 »
Reply with quote Modify message

I think my brain may be past its sell-by date for the day, but I did manage this:

1.  I used your idea from above, but I used it to load a Javascript variable with the contents of a hook.

2.  I used cpdig to replace the comments that frame the "Additional Product Information", with a div that has a unique ID.  Since there's a closing comment, I was able to replace it to close the div.

I figure that when my brain's back, I ought to be able to use Javascript to rewrite the contents of that div using my variable, which contains the html from the hook.

If it works, and if they ever change those comments, the description ought to revert to their original description.  I think.
Logged
Hex Angel - Patrick
Gold Dragon
****

Posts: 500



Our nature lies in movement; complete calm is death.
   

WWW
Re: Advanced String Replacement using cpdig
« Reply #11 on: 11/01/09 at 17:34:50 »
Reply with quote Modify message

Okay, a couple semantic errors in there, and a change to the string replacement. This works, as described above:


Code:
$replace = $html;
$replace =~ s/^.*?<!-- REPLACEMENT (.*?) REPLACEMENT -->.*?$/$1/sgi;
$html =~ s/CafePress Product Description -->.*?<!-- \/CP Product Description/-->$replace<!-- CP Product Description/sgi;
     


Logged

CPSHOP
www.PeaceAndLoveShop.com Namaste
BLOG
www.TheNomadicSpirit.com Inspired by travel
www.GodBeautyPerfectionLove.com Inspired by spirit
www.MyMuseMoviesMusicAndBooks.com Inspired by the arts
Bradley W. Schenck
Gold Dragon
****

Posts: 242



Go Scots!
   

WWW
Re: Advanced String Replacement using cpdig
« Reply #12 on: 11/01/09 at 19:40:14 »
Reply with quote Modify message

Yes!  I had a really odd problem that may have been a cache issue, but on my second try, that worked perfectly.  Thanks!

It should degrade better than the Javascript idea, too, since if they'd changed only the first comment then my idea would have closed out some other div too soon.

Now I just need to come up with the HTML for the new hooks .

I think this could go a long way toward reducing the appearance of duplicate content.  Very neat!
Logged
Hex Angel - Patrick
Gold Dragon
****

Posts: 500



Our nature lies in movement; complete calm is death.
   

WWW
Re: Advanced String Replacement using cpdig
« Reply #13 on: 11/01/09 at 21:24:45 »
Reply with quote Modify message

<smile> Glad to help...

...you know, I love your stuff. Exquisite sense of design, and quite clever with the verbiage as well.

Kudos,

Patrick.
Logged

CPSHOP
www.PeaceAndLoveShop.com Namaste
BLOG
www.TheNomadicSpirit.com Inspired by travel
www.GodBeautyPerfectionLove.com Inspired by spirit
www.MyMuseMoviesMusicAndBooks.com Inspired by the arts
Bradley W. Schenck
Gold Dragon
****

Posts: 242



Go Scots!
   

WWW
Re: Advanced String Replacement using cpdig
« Reply #14 on: 04/30/12 at 22:14:37 »
Reply with quote Modify message

I just cleared one of my caches today only to discover that CP has again made a radical change to the way they're storing images.  The new image address formats look like this:


Code:
http://i1.cpcache.com/product/(PRODUCT NUMBER).jpg?height=150&width=150


and


Code:
http://i1.cpcache.com/product/(PRODUCT NUMBER)/(PRODUCT NAME).jpg?height=240&width=240


That's a weird switch between product numbers and names; I guess I'll have to look at a shop that isn't doing any text replacements to verify it, but hmmm.  Also interesting that the image size is now an argument, which might be part of my problem.

Anyway so far I haven't been able to substitute a new naming convention - I think I changed the path just fine, but those arguments won't change for me, either with the CPShop images file or with CPDig. Pilot error is always a consideration, of course.

Someday I'll have to clear my other caches. There will be great lamentation.
Logged
Pages: 1 2 » Reply Notify of replies Send the topic Print 
Jump to:


marty.net message board (http://www.marty.net/cgi/marty/board/messageboard)

Powered by YaBB 1 Gold - Release (Yet Another Bulletin Board)
Copyright 2000-2001, X-Null. All Rights Reserved.