strrev() and brackets issue?

Discussion in 'PHP' started by monkeyclap, Feb 25, 2010.

  1. #1
    is there an easy way to make it not reverse brackets, i.e. not to convert "(" to ")"

    cheers
     
    monkeyclap, Feb 25, 2010 IP
  2. insert

    insert Peon

    Messages:
    148
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i don't think so, just replace the brackets with the opposite one before using strrev()...
     
    insert, Feb 25, 2010 IP
  3. monkeyclap

    monkeyclap Active Member

    Messages:
    836
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    85
    #3
    nah, that won't work:
    all brackets end up being the same...
     
    monkeyclap, Feb 25, 2010 IP
  4. HostingProvider

    HostingProvider Active Member

    Messages:
    1,480
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    95
    #4
    Try this:
    Does that work for you? Not sure if str_replace() does all the replacements simultaneously or one-after-one, though...
     
    HostingProvider, Feb 26, 2010 IP
  5. monkeyclap

    monkeyclap Active Member

    Messages:
    836
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    85
    #5
    nope, no luck :( just the same as before...
     
    monkeyclap, Feb 26, 2010 IP
  6. HostingProvider

    HostingProvider Active Member

    Messages:
    1,480
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    95
    #6
    Well... there's a way I can think of, but it's not clean and it's not "pretty" :)
    $str=str_replace(array(')','('),array('V67ggbvoA','vfUtFiuF6'),$str);
    $str=strrev($str);
    $str=str_replace(array(strrev('V67ggbvoA'),strrev('vfUtFiuF6')),array(')','('),$str);
    PHP:
    This was just an illustration of what I thought. Of course, you might need to change it to fit your needs.
     
    HostingProvider, Feb 26, 2010 IP
  7. monkeyclap

    monkeyclap Active Member

    Messages:
    836
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    85
    #7
    thanks, will give it a shot, but trying to understand what V67ggbvoA and vfUtFiuF6 is?
     
    monkeyclap, Feb 28, 2010 IP
  8. HostingProvider

    HostingProvider Active Member

    Messages:
    1,480
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    95
    #8
    Just random strings :) You can put whatever you want in there, just make sure it's not something found on the original string.
     
    HostingProvider, Feb 28, 2010 IP
  9. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #9
    you can maybe do something like:

    
    $string = "(My String)";
    
    $string = str_replace("(","_(_",$string);
    $string = str_replace(")","_)_",$string);
    
    $string = strrev($string);
    
    $string = str_replace("_(_",")",$string);
    $string = str_replace("_)_","(",$string);
    
    PHP:
     
    killaklown, Feb 28, 2010 IP
  10. monkeyclap

    monkeyclap Active Member

    Messages:
    836
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    85
    #10
    nope, no luck. both methods still continue to flip the brackets :(

    i'm going to raise the stakes and offer the first person with a working method a $45 google adwords voucher! :D
     
    monkeyclap, Mar 2, 2010 IP
  11. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #11
    you sure? I just tried what I posted and it worked...

    INPUT: (My String)
    OUTPUT: (gnirtS yM)
     
    killaklown, Mar 2, 2010 IP
  12. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #12
    Try using this function instead, you can add more cases for all different brackets

    
    function reverse($var){
    $len=strlen($var)-1;
    for($i=$len; $i>=0; $i--){
    switch ($var[$i]){
    	case "(":
    		$var[$i]=")";
    		break;
    	case ")":
    		$var[$i]="(";
    		break;	   
    }//endswitch
    $rvar.=$var[$i];
    }//end for
    return $rvar;
    }//end function
    
    PHP:
     
    Narrator, Mar 2, 2010 IP
    monkeyclap likes this.
  13. monkeyclap

    monkeyclap Active Member

    Messages:
    836
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    85
    #13
    great stuff, that did the trick. thanks everyone for your help - much appreciated!

     
    monkeyclap, Mar 3, 2010 IP
  14. TYPELiFE

    TYPELiFE Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Neat little snippet to keep on my clipboard
     
    TYPELiFE, Mar 9, 2010 IP