Page 1 of 1

str()

Posted: Wed Jun 23, 2004 10:44 am
by HaVoC

Code: Select all

<?php
$newpostinfo = str_replace('swearword','*****',$newpostinfo); 
?>
Now, that works but when someone puts a capital letter in the mix, it's no longer filtered? Anyone got a solution?

Posted: Wed Jun 23, 2004 10:47 am
by wwwapu
use str_ireplace. Case-insensitive version of replacement function

Posted: Wed Jun 23, 2004 10:50 am
by markl999
str_ireplace is only available in PHP5
For PHP4 the user comments at http://php.net/str_replace have examples of case insensitive replacing.

Posted: Wed Jun 23, 2004 10:50 am
by patrikG
str_ireplace is PHP5 only. Use [php_man]strtolower[/php_man] on both strings.

Edit: gah, Mark beat me to it - yet again :?

Posted: Wed Jun 23, 2004 11:29 am
by feyd
..and there's always the regular expression functions: [php_man]preg_replace[/php_man]() and [php_man]eregi_replace[/php_man]()

Posted: Wed Jun 23, 2004 3:53 pm
by redmonkey
My attempt to emulate str_ireplace under PHP4.....

Code: Select all

<?php
if (!function_exists('str_ireplace'))
{
  function str_ireplace($search, $replace, $subject, $count = 0)
  {
    $subject_array = true;
    if (is_array($search) && is_string($replace))
    {
      $replace = array($replace);
      for ($i = 1, $max = count($search); $i < $max; $i++)
      {
        $replace[] = $replace[0];
      }
    }
        
    $search  = !is_array($search)  ? array($search)  : $search;
    $replace = !is_array($replace) ? array($replace) : $replace;
    if (!is_array($subject))
    {
      $subject = array($subject);
      $subject_array = false;
    }

    for ($i = 0, $sub_size = count($subject); $i < $sub_size; $i++)
    {
      for ($j = 0, $limit = count($search); $j < $limit; $j++)
      {
        $search_str = '/' . preg_quote($search[$j]) . '/i';
        $count += preg_match_all($search_str, $subject[$i], $notused);
        $search_array[] = $search_str;
      }
    }
    $subject = preg_replace($search_array, $replace, $subject);
    
    return $subject_array ? $subject : $subject[0];
  }
}
?>
....may be of use to someone