Right$ Left$ in PHP

Discussion in 'PHP' started by tonyrocks, Jan 13, 2010.

  1. #1
    Ok, so I'm used to string manipulation in ASP (with right and left) but I'm a bit perplexed with PHP.

    So here's the situation...my parents went away on a ...ok sorry. Here is what I have:

    I'm working on a custom login redirect for an existing training cms (sounds like Poodle) and having problems.

    I have a user go to this url:

    /login/index.php?client=FredsDOG

    and login with the following:

    username: chuckABC
    password: whatever

    in the login code, I construct the username this way:

    $normalUser=$frm->username.'||'.$frm->client;

    So now, $normalUser = chuckABC||FredsDOG

    BUT, I want to check if the username has a three digit code tacked at the end of the username and do some manipulation, so:

    If username ends in ABC, then I want to do this

    $normalUser = fredsABC||ABC
    instead of
    $normalUser = chuckABC||FredsDOG

    in asp, i would just take the string, 4 spaces to the left and beyond...but not sure how to do this in PHP. Any ideas?
     
    tonyrocks, Jan 13, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't quite understand what you mean by the manipulation from one to the other, but take a look at the substr() function. Pretty sure that is what you want
     
    JAY6390, Jan 13, 2010 IP
  3. tonyrocks

    tonyrocks Active Member

    Messages:
    1,574
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    88
    #3
    substr() looks like the way to go! So, how do I use it if I want to hack off or snag the last three character of a string...like this:


    $rest = substr("dogABC", ?);

    I would like $rest to return "dog", I would like the assign the characters "ABC" to another variable...like $affix for instance.
     
    tonyrocks, Jan 13, 2010 IP
  4. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    From what I understood, you want to get the last three digits after || in the username?

    JAY6390 was right, you'll want to use substr();

    <?php
    	echo substr('fredsABC||ABC', -3, 3);
    ?>
    PHP:
    Will print out: ABC

    Just replace the string with the variable containing the username.

    Regards,

    Steve
     
    Steve136, Jan 13, 2010 IP
  5. tonyrocks

    tonyrocks Active Member

    Messages:
    1,574
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    88
    #5
    Awesome. This works for me. Thanks for the help. For me I just wanted to snag the 3 characters before the ||, but this makes sense though. Thanks the for explanation.

    This is what worked for me:

    echo substr('fredsABC||ABC', -8, 3);

    However if the length of the right ABC changes, I may be up a creek... I guess what I really should do is find the first instance of || and grab the left 3. I'll figure it out :) Thanks again guys!
     
    Last edited: Jan 13, 2010
    tonyrocks, Jan 13, 2010 IP
  6. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    In that case, it may be better to use explode()

    $stuff = explode('||', 'fredsABC||ABC');

    $stuff[0] will contain fredsABC (the first part) and $stuff[1] will contain ABC (the second part)

    That way it doesn't matter how long they are in length it'll just use the || as a delimiter.

    Regards,

    Steve
     
    Steve136, Jan 13, 2010 IP