Replacing words in php

Discussion in 'PHP' started by mgrohan, Jul 18, 2007.

  1. #1
    I've been searching for days and can't find the information i need.

    Currently i have a script that calls content by:

    <? echo $pagecontent; ?>

    This displays html text in a table form.


    What i want to do now is replace certain terms in the display tables, when the text is called.
    i.e. the table displayed contains many rows and columns, but i only want to change the instances of the text 'apple', 'orange', 'banana'.
    I want to change the above to 'kiwi', mandarin', 'grapes'.

    Therefore the table will display as it's content as usual but all instances of
    apple - will be displayed as kiwi
    orange - will be displayed as mandarin
    grapes - will be displayed as grapes.

    Thanks for any help.
     
    mgrohan, Jul 18, 2007 IP
  2. DavidAusman

    DavidAusman Well-Known Member

    Messages:
    399
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    108
    #2
    You can use array to replace your text
    
    <?php
    function _replacement($text) {
    $array = array("apple" => "kiwi", "banana" => "grapes", "orange" => "mandarin");
    foreach($array as $original => $replacement) {
    $text = str_replace($original, $replacement, $text);
    }
    return $text;
    }
    
    // Whenever you need to replace those words, call _replacement()
    _replacement($text);
    ?>
    
    PHP:
     
    DavidAusman, Jul 18, 2007 IP