|
-
Jul 1st, 2011, 05:30 AM
#1
Thread Starter
Fanatic Member
[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.
-
Jul 1st, 2011, 11:34 AM
#2
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);
-
Jul 1st, 2011, 11:50 AM
#3
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|