Insensitive Case for str_replace

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    Insensitive Case for str_replace

    Well after many hours of writting a "sanitizing " function I have discovered I do not have stri_replace installed or something. I don't really want to search for WHERE, WhERE, WheRE... and so on, just to remove WHERE for MySQL input, does anyone have an idea how I could get around it?

    I was using ronverdonk's code:
    [PHP]str_ireplace( array(" ", "/",",","'","*"," and","or","wher e"),'', 'WHERE This IS a TeSt.' ) );[/PHP]
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    When you do not have PHP5, you can use your own str_ireplace function. Thanks to n00b at battleofthebits in the PHP documentation. [php]if (!function_exis ts('str_ireplac e') {
    function str_ireplace($s earch,$replace, $subject) {
    $token = '^[[term^]';
    $haystack = strtolower($sub ject);
    $needle = strtolower($sea rch);
    while (($pos=strpos($ haystack,$needl e))!==FALSE) {
    $c++;
    $subject = substr_replace( $subject,$token ,$pos,strlen($s earch));
    $haystack = substr_replace( $haystack,$toke n,$pos,strlen($ search));
    }
    while (($pos=strpos($ subject,$token) )!==FALSE) {
    $subject = substr_replace( $subject,$repla ce,$pos,strlen( $token));
    }
    return $subject;
    }
    } [/php]Ronald

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      Actually I found this:
      [PHP]eregi_replace( array(" ", "/",",","'","*"," and","or","wher e"),'', 'WHERE This IS a TeSt.' )[/PHP]

      And it works except for the pattern to replace. In there it is an array, but I believe it needs to be in another form.
      [PHP]eregi_replace( "where",'', 'WHERE This IS a TeSt.' )[/PHP]
      works, and the php manual says it has to be a POSIX extended regular expression, so, I can do single characters, but not a set of character or a set of words. Any advice or hints?

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        No hints. But why don't you want to use the str_ireplace function I posted instead of using a reg exp?

        Ronald

        Comment

        • TheServant
          Recognized Expert Top Contributor
          • Feb 2008
          • 1168

          #5
          Originally posted by ronverdonk
          No hints. But why don't you want to use the str_ireplace function I posted instead of using a reg exp?

          Ronald
          Because if I do, it converts all the string to lower. I want to retain capitals in names. Acually, I have just thought about a problem. Say someone's name is "Wherever_you_g o" that will remove it? I might ahve to think of a better system which allows that. The reg exp doesn't need to convert to lowercase.

          Comment

          Working...