Results 1 to 3 of 3

Thread: [RESOLVED] preg_replace Problem

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Resolved [RESOLVED] preg_replace Problem

    Hi guys,

    Im trying to replace a words that are entered into a settings field (seperated by a ,)

    So in my admin panel I enter words in the "hide_words" setting field like so: hello,goodbye,tommy,today

    Heres my example:

    PHP Code:
    $words explode(",",settings['hide_words']);

    $replace "<b>[Disallowed]</b>";
    $message preg_replace("#$words#""$replace"$message); 
    Doesnt seem to be replacing the words.

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: preg_replace Problem

    The pattern in preg_replace() has to be a string; you can't create a string by sticking an array in it like you've got there. You can either take $words and create a string via implode(), like so:
    PHP Code:
    $words explode(",",$settings['hide_words']);
    $replace "<b>[Disallowed]</b>";
    $message preg_replace("#".implode("|",$words)."#"$replace$message); 
    ...or you could save your variable the trauma of an explode/implode by doing a str_replace() instead:
    PHP Code:
    $replace "<b>[Disallowed]</b>";
    $message preg_replace("#".str_replace(",","|",$settings['hide_words'])."#"$replace$message); 

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: preg_replace Problem

    I used the second method and it works perfeclty, thanks alot SambaNeko +1

Posting Permissions

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



Click Here to Expand Forum to Full Width