Page 1 of 4 123 ... LastLast
Results 1 to 10 of 32
  1. #1
    Join Date
    Nov 2009
    Location
    UK
    Posts
    1,141
    Plugin Contributions
    0

    Default 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?

  2. #2
    Join Date
    Jan 2010
    Location
    Australia
    Posts
    244
    Plugin Contributions
    1

    Default Re: Replacing a text string on a page

    everything is possible dgent, problem is you are looking at modifying quite a few pages.
    Believe it or not But My existence is illusional

  3. #3
    Join Date
    May 2010
    Location
    WA State
    Posts
    1,650
    Plugin Contributions
    1

    Default Re: Replacing a text string on a page

    I'm pretty sure that what you want to do would require editing the database.
    Bumble Bee Template - my first contribution!

    The best way to learn is to try stuff. But, back it up first.

  4. #4
    Join Date
    Jul 2005
    Location
    Upstate NY
    Posts
    21,820
    Plugin Contributions
    25

    Default 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.
    Last edited by gjh42; 16 Dec 2011 at 07:49 AM.

  5. #5
    Join Date
    Jul 2005
    Location
    Upstate NY
    Posts
    21,820
    Plugin Contributions
    25

    Default 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.

  6. #6
    Join Date
    Nov 2009
    Location
    UK
    Posts
    1,141
    Plugin Contributions
    0

    Default 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.

  7. #7
    Join Date
    Nov 2009
    Location
    UK
    Posts
    1,141
    Plugin Contributions
    0

    Default Re: Replacing a text string on a page

    Quote Originally Posted by gjh42 View Post
    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.

  8. #8
    Join Date
    Nov 2009
    Location
    UK
    Posts
    1,141
    Plugin Contributions
    0

    Default 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-->

  9. #9
    Join Date
    Jul 2005
    Location
    Upstate NY
    Posts
    21,820
    Plugin Contributions
    25

    Default 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.

  10. #10
    Join Date
    Jul 2005
    Location
    Upstate NY
    Posts
    21,820
    Plugin Contributions
    25

    Default 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;
    }
    ?>
    Last edited by gjh42; 16 Dec 2011 at 05:25 PM.

 

 
Page 1 of 4 123 ... LastLast

Similar Threads

  1. Increase maximum search text string?
    By igofresh in forum General Questions
    Replies: 0
    Last Post: 23 Feb 2011, 03:29 AM
  2. How to find a text string
    By thomasw98 in forum General Questions
    Replies: 11
    Last Post: 2 Mar 2010, 10:05 PM
  3. Is this possible? replacing text with image?
    By pb4 in forum Templates, Stylesheets, Page Layout
    Replies: 20
    Last Post: 23 Dec 2009, 04:07 PM
  4. “add to cart:” text string
    By gsh in forum Templates, Stylesheets, Page Layout
    Replies: 1
    Last Post: 3 Oct 2006, 07:20 PM

Bookmarks

Posting Permissions

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