str_ireplace 与 str_replace 区别在于不区分大小写就和
preg_replace("/%body%/", "black", "<body text=%BODY%>");

preg_replace("/%body%/i", "black", "<body text=%BODY%>");
的区别是一样的

解决方案 »

  1.   

    以下取自PEAE的PHP_Compat-1.4.1 -> str_ireplace.php<?php
    // +----------------------------------------------------------------------+
    // | PHP Version 4                                                        |
    // +----------------------------------------------------------------------+
    // | Copyright (c) 1997-2004 The PHP Group                                |
    // +----------------------------------------------------------------------+
    // | This source file is subject to version 3.0 of the PHP license,       |
    // | that is bundled with this package in the file LICENSE, and is        |
    // | available at through the world-wide-web at                           |
    // | http://www.php.net/license/3_0.txt.                                  |
    // | If you did not receive a copy of the PHP license and are unable to   |
    // | obtain it through the world-wide-web, please send a note to          |
    // | [email protected] so we can mail you a copy immediately.               |
    // +----------------------------------------------------------------------+
    // | Authors: Aidan Lister <[email protected]>                                |
    // +----------------------------------------------------------------------+
    //
    // $Id: str_ireplace.php,v 1.18 2005/01/26 04:55:13 aidan Exp $
    /**
     * Replace str_ireplace()
     *
     * @category    PHP
     * @package     PHP_Compat
     * @link        http://php.net/function.str_ireplace
     * @author      Aidan Lister <[email protected]>
     * @version     $Revision: 1.18 $
     * @since       PHP 5
     * @require     PHP 4.0.0 (user_error)
     * @note        count not by returned by reference, to enable
     *              change '$count = null' to '&$count'
     */
    if (!function_exists('str_ireplace')) {
        function str_ireplace($search, $replace, $subject, $count = null)
        {
            // Sanity check
            if (is_string($search) && is_array($replace)) {
                user_error('Array to string conversion', E_USER_NOTICE);
                $replace = (string) $replace;
            }        // If search isn't an array, make it one
            if (!is_array($search)) {
                $search = array ($search);
            }
            $search = array_values($search);        // If replace isn't an array, make it one, and pad it to the length of search
            if (!is_array($replace)) {
                $replace_string = $replace;            $replace = array ();
                for ($i = 0, $c = count($search); $i < $c; $i++) {
                    $replace[$i] = $replace_string;
                }
            }
            $replace = array_values($replace);        // Check the replace array is padded to the correct length
            $length_replace = count($replace);
            $length_search = count($search);
            if ($length_replace < $length_search) {
                for ($i = $length_replace; $i < $length_search; $i++) {
                    $replace[$i] = '';
                }
            }        // If subject is not an array, make it one
            $was_array = false;
            if (!is_array($subject)) {
                $was_array = true;
                $subject = array ($subject);
            }        // Loop through each subject
            $count = 0;
            foreach ($subject as $subject_key => $subject_value) {
                // Loop through each search
                foreach ($search as $search_key => $search_value) {
                    // Split the array into segments, in between each part is our search
                    $segments = explode(strtolower($search_value), strtolower($subject_value));                // The number of replacements done is the number of segments minus the first
                    $count += count($segments) - 1;
                    $pos = 0;                // Loop through each segment
                    foreach ($segments as $segment_key => $segment_value) {
                        // Replace the lowercase segments with the upper case versions
                        $segments[$segment_key] = substr($subject_value, $pos, strlen($segment_value));
                        // Increase the position relative to the initial string
                        $pos += strlen($segment_value) + strlen($search_value);
                    }                // Put our original string back together
                    $subject_value = implode($replace[$search_key], $segments);
                }            $result[$subject_key] = $subject_value;
            }        // Check if subject was initially a string and return it as a string
            if ($was_array === true) {
                return $result[0];
            }        // Otherwise, just return the array
            return $result;
        }
    }?>
      

  2.   

    才学习PHP .分都用光了。strpos 有没有不区分大小写的? 谢谢、等等在揭帖
      

  3.   

    唉,都已经告诉你出处了,还不知道去找PEAR的PHP_Compat-1.4.1 -> stripos.php
      

  4.   

    我当然自己出找了。可以找不到呀。 http://aspn.activestate.com/ASPN/Mail/Message/pear-cvs/2087863 这个是你给我的。请再帮帮个忙吧
      

  5.   

    如果效率不是关键的话,楼主可以考虑用正则的函数来代替它?比如eregi_replace或者preg_replace加修饰符i!
      

  6.   

    http://pear.php.net/package/PHP_CompatPackage Information: PHP_Compat
    Provides missing functionality for older versions of PHP