Zen Cart Logo
Forums / General Questions / Replacing a text string on a page

Replacing a text string on a page

Views: 2,167

Results 1 to 20 of 32
14 Dec 2011, 10:40 AM
#1
dgent avatar

dgent

Totally Zenned

Join Date:
Nov 2009
Location:
UK
Posts:
1,117
Plugin Contributions:
0

Replacing a text string on a page

Is there any way of replacing a string on a page, via PHP without having to modify the database in anyway...?

Basically I have a site that sells a certain type of dress, and I want to make a duplicate site on another domain name using the same database, but at the moment on one site where every dress is a type of 'body con dress' I want every product, heading etc that contains the word 'body con' to say 'Going Out' instead.

Is this even possible?

16 Dec 2011, 12:35 AM
#2
gaurav10feb avatar

gaurav10feb

Zen Follower

Join Date:
Jan 2010
Location:
Australia
Posts:
243
Plugin Contributions:
1

Re: Replacing a text string on a page

everything is possible dgent, problem is you are looking at modifying quite a few pages.

16 Dec 2011, 12:37 AM
#3
rescoccc avatar

rescoccc

Totally Zenned

Join Date:
May 2010
Location:
WA State
Posts:
1,700
Plugin Contributions:
2

Re: Replacing a text string on a page

I'm pretty sure that what you want to do would require editing the database.

16 Dec 2011, 6:46 AM
#4
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Replacing a text string on a page

Actually, the stated replacement would probably best be done with PHP. A lot of files would have to be modified, but you could add a snippet everywhere that there may be an instance of "body con" which would use a str_replace() function on the text content before outputting it. You would need to have arrays of old and new text versions (capitalized and lowercase) so that Body con was replaced by Going out, etc. It would be simplest to write a small function with a name like going_out() which would contain the full-length str_replace() code.

It would probably also be worthwhile to do a conversion pass on the define files for the Going Out site, so that fewer template files needed to be edited.

16 Dec 2011, 6:52 AM
#5
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Replacing a text string on a page

I don't know if javascript is capable of handling this, but I suspect it could do much or all of it without modifying PHP files, if you know the right js to use.

16 Dec 2011, 10:50 AM
#6
dgent avatar

dgent

Totally Zenned

Join Date:
Nov 2009
Location:
UK
Posts:
1,117
Plugin Contributions:
0

Re: Replacing a text string on a page

Hi

Ive already been able to do this with Javascript, but obv it changes the text after the page has loaded...hence a Google bot never sees this change and has it as the same content as the first website.

What Im trying to do, it make multiple sites, using the same database (as sizes and styles have to be updated daily - so its alot of time making changes to multiple sites all day long) but have the main keyword terms changed to various popular ones in Google searches.

Copying over images for new products is simple obviously, its just the cutting down on the product editing time which is the main reason, and having multiple ranking sites.

It also means that all of the sites have the same customer database too.

16 Dec 2011, 3:30 PM
#7
dgent avatar

dgent

Totally Zenned

Join Date:
Nov 2009
Location:
UK
Posts:
1,117
Plugin Contributions:
0

Re: Replacing a text string on a page

gjh42:

Actually, the stated replacement would probably best be done with PHP. A lot of files would have to be modified, but you could add a snippet everywhere that there may be an instance of "body con" which would use a str_replace() function on the text content before outputting it. You would need to have arrays of old and new text versions (capitalized and lowercase) so that Body con was replaced by Going out, etc. It would be simplest to write a small function with a name like going_out() which would contain the full-length str_replace() code.

It would probably also be worthwhile to do a conversion pass on the define files for the Going Out site, so that fewer template files needed to be edited.

Id only really want to change the product titles and the category titles. I wouldnt think the define files would be an issue, as these are hard coded files on the server, not part of the database. They only need to be changed once.

16 Dec 2011, 3:48 PM
#8
dgent avatar

dgent

Totally Zenned

Join Date:
Nov 2009
Location:
UK
Posts:
1,117
Plugin Contributions:
0

Re: Replacing a text string on a page

Well Ive done it on the product page by putting this code into tpl_product_info_display.php to replace the product name section

<!--bof ModifiedProduct Name--> <?php // The text string $newprodname = $products_name; // The word we want to replace $oldWord = "Word1"; // The new word we want in place of the old one $newWord = "Word2"; // Run through the text and replaces all occurrences of $oldText $newprodname = str_replace($oldWord , $newWord , $newprodname); ?> <h1 id="productName" class="productGeneral"> <?php echo $newprodname; ?></h1> <!--eof Modified Product Name-->
16 Dec 2011, 4:06 PM
#9
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Replacing a text string on a page

So descriptions won't need to be changed? How about meta data stored in the db? Will any of that have "body con" in it?

Yes, running a script on the set of language define files in maybe Notepad++ or something should be effective there. Adding the transform function to the product/category name output locations ought to be doable; you just need to get all locations like shopping cart, checkout and e-mail sending files.
If you are going to do the same thing for more than one extra site, I would make the function generic and define the replacement string array in a constant which you could redefine for each new site without touching the embedded switchers.

16 Dec 2011, 4:21 PM
#10
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Replacing a text string on a page

Good proof of concept. Now to extract it into a function so you only need to change ```php

<!--bof Product Name--> <h1 id="productName" class="productGeneral"><?php echo $products_name; ?></h1> <!--eof Product Name--> ``` to ```php <!--bof Modified Product Name--> <h1 id="productName" class="productGeneral"><?php echo text_swap($products_name); ?></h1> <!--eof Modified Product Name--> ``` In /includes/functions/extra_functions/text_swap_functions.php ```php <?php // The text string //$newprodname = $products_name;

function text_swap($text) {
$old = array('Body Con', 'body con');
$new = array('Going Out', 'going out');
// Run through the text and replace all occurrences of $oldText
$newtext = str_replace($old , $new , $text);
return $newtext;
}
?>

16 Dec 2011, 4:21 PM
#11
dgent avatar

dgent

Totally Zenned

Join Date:
Nov 2009
Location:
UK
Posts:
1,117
Plugin Contributions:
0

Re: Replacing a text string on a page

gjh42:

So descriptions won't need to be changed? How about meta data stored in the db? Will any of that have "body con" in it?

Yes, running a script on the set of language define files in maybe Notepad++ or something should be effective there. Adding the transform function to the product/category name output locations ought to be doable; you just need to get all locations like shopping cart, checkout and e-mail sending files.
If you are going to do the same thing for more than one extra site, I would make the function generic and define the replacement string array in a constant which you could redefine for each new site without touching the embedded switchers.

Ive done the product descriptions too now... :)

I need some help on the product listings and the category sideboxes, new and featured etc., can't seem to get the string replacements to work, any tips?

This could be a really good mod for making extra sales and for further SEO.

16 Dec 2011, 4:32 PM
#12
dgent avatar

dgent

Totally Zenned

Join Date:
Nov 2009
Location:
UK
Posts:
1,117
Plugin Contributions:
0

Re: Replacing a text string on a page

gjh42:

Good proof of concept. Now to extract it into a function so you only need to change ```php

<!--bof Product Name--> <h1 id="productName" class="productGeneral"><?php echo $products_name; ?></h1> <!--eof Product Name--> ``` to ```php <!--bof Modified Product Name--> <h1 id="productName" class="productGeneral"><?php echo text_swap($products_name); ?></h1> <!--eof Modified Product Name--> ``` > In /includes/functions/extra_functions/text_swap_functions.php > ```php <?php // The text string //$newprodname = $products_name;

function text_swap($text) {
$old = array('Body Con', 'body con');
$new = array('Going Out', 'going out');
// Run through the text and replace all occurrences of $oldText
$newtext = str_replace($old , $new , $text);
return $newtext;
}
?>


I made the extra file and put it in the extra_functions file and changed the code in tpl_prod_display etc but it didnt seem to work ?
16 Dec 2011, 4:40 PM
#13
dgent avatar

dgent

Totally Zenned

Join Date:
Nov 2009
Location:
UK
Posts:
1,117
Plugin Contributions:
0

Re: Replacing a text string on a page

How do I mod using my code a line like this below in the product listings...

<h1 id="productListHeading"><?php echo $breadcrumb->last(); ?></h1>
16 Dec 2011, 4:43 PM
#14
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Replacing a text string on a page

Post an example of the original code and your replacement attempt for areas where it is not working for you.

and changed the code in tpl_prod_display etc but it didnt seem to work ?What did you use in tpl_product_info_display.php?

16 Dec 2011, 4:47 PM
#15
dgent avatar

dgent

Totally Zenned

Join Date:
Nov 2009
Location:
UK
Posts:
1,117
Plugin Contributions:
0

Re: Replacing a text string on a page

gjh42:

Post an example of the original code and your replacement attempt for areas where it is not working for you.

What did you use in tpl_product_info_display.php?

For the product listing...tpl_index_product_list.php

Previously only this...

<h1 id="productListHeading"><?php echo $breadcrumb->last(); ?></h1>

Which I changed to this..

<?php // The text string $breadcrumb2 = $breadcrumb; // The word we want to replace $oldWord = "word1"; // The new word we want in place of the old one $newWord = "word2"; // Run through the text and replaces all occurrences of $oldText $breadcrumb2 = str_replace($oldWord , $newWord , $breadcrumb2); ?> <h1 id="productListHeading"><?php echo $breadcrumb2->last(); ?></h1>

..and for tpl_product_info_display, the code you posted..

<!--bof Modified Product Name--> <h1 id="productName" class="productGeneral"><?php echo text_swap($products_name); ?></h1> <!--eof Modified Product Name-->
16 Dec 2011, 4:49 PM
#16
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Replacing a text string on a page

See http://php.net/manual/en/function.str-replace.php for info on the str-replace() function.

So did the product name return the original unchanged, or what?

16 Dec 2011, 4:53 PM
#17
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Replacing a text string on a page

<?php echo $breadcrumb->last(); ?></h1>

Which I changed to this..

<?php // The text string $breadcrumb2 = $breadcrumb;$breadcrumb is not the same as $breadcrumb->last().
16 Dec 2011, 4:55 PM
#18
dgent avatar

dgent

Totally Zenned

Join Date:
Nov 2009
Location:
UK
Posts:
1,117
Plugin Contributions:
0

Re: Replacing a text string on a page

gjh42:

$breadcrumb is not the same as $breadcrumb->last().

You're right, and Ive fixed it now :)

16 Dec 2011, 5:04 PM
#19
rescoccc avatar

rescoccc

Totally Zenned

Join Date:
May 2010
Location:
WA State
Posts:
1,700
Plugin Contributions:
2

Re: Replacing a text string on a page

Doesn't this approach add quite a bit of overhead?

16 Dec 2011, 5:08 PM
#20
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Replacing a text string on a page

If done efficiently, I wouldn't think it would add more overhead than many other mods.

Obviously the piecemeal coding approach will require an immense amount of maintenance for more than one extra site, or version upgrades.