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?
Re: Replacing a text string on a page
everything is possible dgent, problem is you are looking at modifying quite a few pages.
Re: Replacing a text string on a page
I'm pretty sure that what you want to do would require editing the database.
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.
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.
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.
Re: Replacing a text string on a page
Quote:
Originally Posted by
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.
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-->
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.
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 Code:
<!--bof Product Name-->
<h1 id="productName" class="productGeneral"><?php echo $products_name; ?></h1>
<!--eof Product Name-->
to
PHP Code:
<!--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 Code:
<?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;
}
?>