str()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
HaVoC
Forum Commoner
Posts: 83
Joined: Sat Feb 07, 2004 7:20 am
Location: Smiths Falls, CA

str()

Post 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?
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

use str_ireplace. Case-insensitive version of replacement function
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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 :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

..and there's always the regular expression functions: [php_man]preg_replace[/php_man]() and [php_man]eregi_replace[/php_man]()
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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
Post Reply