Results 1 to 2 of 2

Thread: ereg_replace multiple strings problem

  1. #1
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default ereg_replace multiple strings problem

    Hi, I have a script that has special characters that need to be replaced with an image, to be inserted into mysql. I use ereg_replace, but it will only replace one string, when I have multiple strings that need to be replace, before it gets inserted into mysql. the code looks like this:

    PHP Code:
    <?php

    $title 
    $_POST['title'];
    $message2 $_POST['message'];
    $uname $_COOKIE['uname'];

    if(
    $a == 'a'){
    mysql_query("INSERT INTO topics (title, message, uname) VALUES('$title', '$message', '$uname') ") or die(mysql_error());

    //Notice how it will replace this string, but I have more than one
    //string like ":D" (made to represent emoticons)

    $smstring "<img src='laugh.gif'>";

    $message ereg_replace(":D""$smstring""$message2");

    }else{

    ?>
    <form action="post.php?a=a" method="post">
    <input type="text" name="title" value="title"><br />
    <textarea name="message" value="message"><br />
    <input type="submit" name="submit">
    </form>
    <?php
    }
    ?>
    Of course, it does work, but it will only replace one string with emoticons...I tried repeating the variable ( $message ) twice, but all failed...how do I get it to change multiple strings??
    Last edited by JShor; 09-01-2007 at 07:54 PM. Reason: typo on emoticon

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

    Default

    Because you haven't applied the global modifier. However, regex is a waste of resources here; it would be better to do
    Code:
    $message = str_replace(':D', $smstring, $message2);
    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
  •