Results 1 to 2 of 2

Thread: String Replacement

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String Replacement

    Ok, I am trying to make a string replacer to do what forums do with replacing BBC code and smilies. Here is what I have:
    PHP Code:
    <?php require('config.php'); ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Posts</title>
    </head>

    <body>
    <?php
        $get 
    mysql_query("SELECT * FROM `messages` ORDER BY id DESC") or die (mysql_error());
        while (
    $p mysql_fetch_array($get)) {
        
            
    $get_replace mysql_query("SELECT * FROM `replacements`") or die ('Error getting replacements! <br />' .mysql_error());
            while (
    $r mysql_fetch_array($get_replace)) {
                
    extract($r);
                
    $post str_replace($tbefore$tafter$p['message']);
            }
            
            echo 
    '
            <b>'
    .$p['title'].'</b>
                <br />
            '
    .$post.'';
        }
        
    ?>
    </body>
    </html>
    I have 3 things to replace in the database, but it only replaces one of them. I thought that this would be fixed with the while statement, but still nothing. Any ideas? thanks
    Thanks DD, you saved me countless times

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    That's the wrong MIME type for XHTML.

    Your problem is here:
    Code:
                $post = str_replace($tbefore, $tafter, $p['message']);
    Say you have replacements:
    Code:
    +---------+--------+
    | tbefore | tafter |
    +---------+--------+
    | :)      | smile  |
    | :(      | frown  |
    +---------+--------+
    and a post (stored in $p['message']):
    Code:
    Lorem :) ipsum dolor :( sit amet...
    That line will be executed once for each replacement. So, first of all, it takes $p['message'], replaces each :) with smile:
    Code:
    Lorem smile ipsum dolor :( sit amet...
    ... and stores the result in $post. Then, it takes $p['message'] again (which is unmodified: remember, the result of the last loop was stored in $post), and applies the next replacement:
    Code:
    Lorem :) ipsum dolor frown sit amet...
    You want to store the results back into $p['message']:
    Code:
                $p['message'] = str_replace($tbefore, $tafter, $p['message']);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •