Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts

    Arrow str_replace() problems

    Hi Folks,

    In the message forum I have built, I have added str_replace to void out naughty words!!! (LOL)

    Here is what I have:

    PHP Code:
    $result mysql_db_query($db"SELECT * FROM words WHERE id>0");
    while (
    $row mysql_fetch_array($result)) {
         
    $word $row["word"];
         
    $message str_replace($word,"****",$message);

    Problem is, lets say the value of $word = cat. If the $message has 'cat' in it then it replaces it with '****'. Problem is, if the message contains 'Cat' or 'cat!' etc it won't replace it!

    Is there any way around this other than say making the entire message lower case.

    Thanks in advance!
    Taylor.

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this code instead:
    PHP Code:
     $result mysql_db_query($db"SELECT * FROM words WHERE id>0"); 
     while (
    $row mysql_fetch_array($result)) { 
      
    $word $row["word"]; 
      
    $message preg_replace("/($word)/i","****",$message);
     } 
    http://ca2.php.net/preg_replace

    Sadiq.

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or just use the case insensitive version of str_replace may be?

    PHP Code:
    $result mysql_db_query($db"SELECT * FROM words WHERE id>0");
    while (
    $row mysql_fetch_array($result)) {
         
    $word $row["word"];
         
    $message str_ireplace($word,"****",$message);


  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Way to revive a 7 year old thread! haha, but you do have a point - your solution to the problem was a lot better than preg_replace.

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    They joined for that? - a 7 year old thread?.. well.. thats dedication i suppose!
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

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