|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi,
I need a case-insensitive replace function to run a string. I've tried to substitute eregi_replace for str_replace, but it doesn't seem to work with my array of replaceable terms (it did work on a simple test, however). I'm taking a string submitted from a form and flagging words that may not comply with a set of standards, so that an editor can easily spot them and decide what to do with them. Right now it's set-up pretty much like this on my test page: <? //simulated string from the form $ad_text = "THIS STRING IS EMPLOYED TO FLAG REQUIRED LIST OF UNLAWFUL SINGLE TERMS!"; ?> Unfiltered Text: <?= $ad_text ?> <? $bad_words = array('employed', 'law', 'required', 'single'); $flag_bad_words = array('employed**', 'law**', 'required**', 'single**'); //works, but only case-sensitive //$ad_text = str_replace($bad_words, $flag_bad_words, $ad_text); //doesn't work at all $ad_text = eregi_replace($bad_words, $flag_bad_words, $ad_text); ?> <br /> Filtered Text: <?= $ad_text ?> </body> From the example I saw it appeared that the two function were interchangeble. I guess not. (I realize that it's probably possible to set-up a loop to go through a single array of terms and add the "**" where applicable, but I haven't gotten to that yet. Right now I just want make sure that no matter how the submitter types the words, if they appear in the string they get flagged before they come out on the other side.) Any idea what I'm doing wrong? Thanks. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
"Pupkin" <spamagnet@dorrk.com> wrote in message news:MPG.219be6b2d39c0c1f9897e6@news.giganews.com. .. > Hi, > > I need a case-insensitive replace function to run a string. <snip> > From the example I saw it appeared that the two function were > interchangeble. I guess not. very much NOT. > (I realize that it's probably possible to set-up a loop to go through a > single array of terms and add the "**" where applicable, but I haven't > gotten to that yet. Right now I just want make sure that no matter how > the submitter types the words, if they appear in the string they get > flagged before they come out on the other side.) > > Any idea what I'm doing wrong? you're using eregi_* first of all! see if this s: <? $test = 'THIS STRING IS EMPLOYED TO FLAG REQUIRED LIST OF UNLAWFUL SINGLE TERMS!'; $test = preg_replace('/\b(employed|law|required|single)\b/i', '$1**', $test); echo '<pre>' . print_r($test, true) . '</pre>'; ?> notice 'unlawful' is skipped...as would 'singleton'. the whole word is considered here. |
|
![]() |
| Outils de la discussion | |
|
|