#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    62
    Rep Power
    13

    How-to lowercase string except " OR "?


    Can anyone show me, I'm assuming via a preg_match, how to lowercase an entire string except for any uppercase OR with one or more spaces on both sides of it?

    ie. " OR " or... " OR "

    ie. [hEllO WoRLD hOW OR plENty OR]
    to. [hello world how OR plenty or]

    thanks.
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,760
    Rep Power
    9397
    As I posted in the PHP thread,
    Code:
    /(.*?)((?: OR )?)/
    Use a callback - either with preg_replace() and /e or preg_replace_callback() - that lowercases $1 and appends $2 (which might be an empty string).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    170
    Rep Power
    54
    Regex is usually above my head. If you're more familiar with PHP, this might work for you.

    PHP Code:
    $string "hEllO WoRLD hOW OR plENty OR";
    $exp explode(" "$string);
    $count count($exp);
    $i=0;
    while((
    $i+1)<$count){
         if(
    $exp[$i] != "OR"){
              
    $mod[$i] = strtolower($exp[$i]);
         }else{
              
    $mod[$i] = $exp[$i];
         }
         
    $i=$i+1;
    }
    while((
    $i+1)==$count){
         
    $mod[$i] = strtolower($exp[$i]);
         
    $i=$i+1;
    }
    $new implode(" "$mod); 
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    62
    Rep Power
    13
    Originally Posted by requinix
    As I posted in the PHP thread,
    Code:
    /(.*?)((?: OR )?)/
    Use a callback - either with preg_replace() and /e or preg_replace_callback() - that lowercases $1 and appends $2 (which might be an empty string).
    hmmm... when I run the preg_match using that regex it doesn't get any results?


    Code:
    $string = 'change email OR password "hello OR world OR"';
    
    preg_match(
    	 "/(.*?)((?: OR )?)/e"
    	,$string
    	,$matches
    );
    
    print_r($matches);
    
    Array
    (
        [0] => 
        [1] => 
        [2] => 
    )
  8. #5
  9. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,760
    Rep Power
    9397
    If you want to see the matches then use
    Code:
    /(.*?)( OR |$)/
    instead.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    62
    Rep Power
    13
    Ended up with 3 solutions which all work:

    Code:
    $string = "hEllO WORld hOwOR OR plENty OR";
    
    # preg_replace_callback
    $string1 =	preg_replace_callback(
    				 "/(.*?)((?: OR )?)/"
    				,create_function(
    					 '$m'
    					,'return strtolower($m[1]).$m[2];'
    				)
    				,$string
    			);
    
    # preg_replace
    $string2 =	preg_replace(
    				 "/(.*?)((?: OR )?)/e"
    				,"strtolower('\\1').'\\2'"
    				,preg_quote($string)
    			);
    $string2 = stripslashes($string2);
    
    # preg_match_all
    preg_match_all(
    	 "/(?: OR )/"
    	,$string
    	,$matches
    	,PREG_OFFSET_CAPTURE
    );
    $string3 = strtolower($string);
    foreach ($matches[0] as $m ) {
    	$string3 = substr_replace($string3,$m[0],$m[1],count(str_split($m[0])));
    }
    
    print "<pre>";
    var_dump($string);
    var_dump($string1);
    var_dump($string2);
    var_dump($string3);
    print "</pre>";
    
    string(30) "hEllO WORld hOwOR OR plENty OR"
    string(30) "hello world howor OR plenty or"
    string(30) "hello world howor OR plenty or"
    string(30) "hello world howor OR plenty or"
  12. #7
  13. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93

    compact solution


    Hmmm...

    Here is another method, no callback, quite compact.
    Tested this code, it works.


    Code:
    <?php
    $string='hEllO WoRLD hOW OR plENty or hi OR hEy'; 
    $regex=',(?i)((?!\bOR).*?)(?-i:(\bOR\b)|$),';
    preg_match_all($regex, $string, $matches,PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER);
    $s=count($matches[1]);
    $converted='';
    for ($i=0;$i<$s;$i++) {
    $converted.=strtolower($matches[1][$i][0]).$matches[2][$i][0];
    }
    echo $converted."<br />";
    ?>
    The output:

    hello world how OR plenty or hi OR hey

IMN logo majestic logo threadwatch logo seochat tools logo