WEB Replace Text in page?

Discussion in 'OT Technology' started by DillDoe, Mar 3, 2008.

  1. DillDoe

    DillDoe Feels good mang

    Joined:
    Jan 16, 2004
    Messages:
    62,315
    Likes Received:
    46
    Location:
    dilldoe.organ
    Trying to make a link/button (in php) that'll replace text on a page when clicked.

    I tried using the str_replace function but stuck on how to apply it to a page (index.php) instead of just a text string.

    any suggestions?
     
  2. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,918
    Likes Received:
    10
    Location:
    Los Angeles
    You "apply" the string to the page by outputting it to the page.

    Code:
    $string = "The cheese is old and moldy.";
    $newstring = str_replace("cheese", "cake", $string);
    print $newstring; // The cake is old and moldy.
    
    You can't make a link or button in PHP. You can, however, generate the mark-up and output the link or button. I don't think the above solution is what you are looking for, as it will only replace chars/strings that are identical to the old string. You'll have to have a variable that contains the entire text to be replaced and a variable that contains the text that will replace it. Then when the user clicks the link/button, ie, passes the GET variable and the page reloads, an if/else will decide which string to display.

    Or you could just use JavaScript and replace it on the fly.
     
  3. DillDoe

    DillDoe Feels good mang

    Joined:
    Jan 16, 2004
    Messages:
    62,315
    Likes Received:
    46
    Location:
    dilldoe.organ
    I'm still not wrapping my head around that code :hs:

    Don't I need $string to be all the text in the page?
     
  4. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    Explain a bit better what you're trying to do?
     
  5. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,918
    Likes Received:
    10
    Location:
    Los Angeles
    Ideally, yes, you would. But to accomplish what you want this is may not be the best solution. But if you insist on trying it this way, here you go:

    Code:
    $string_o = "The cheese is old and moldy.";
    $string_n = "I love cake.";
    $newstring = str_replace($string_o, $string_n, $string_o);
    
    print $newstring; .
    
    $string_o will contain the text to be replaced, $string_n will contain the text to replace $string_o.
     
  6. DillDoe

    DillDoe Feels good mang

    Joined:
    Jan 16, 2004
    Messages:
    62,315
    Likes Received:
    46
    Location:
    dilldoe.organ
    I figured it out. I was editing the wrong file, but I found the right one and it was as simple as just doing a str_replace :mamoru:
     

Share This Page