SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    Rusted & Weathered HumanClay's Avatar
    Join Date
    Sep 2000
    Location
    Milwaukee, WI
    Posts
    225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to strip the last character in a variable in PHP. HOw do I do this? i.e.

    $var = "words";

    I want to be able to strip that "s" off the end of the word. I forgot how to do this ... Thanks.
    Steve Caponetto - [profile] [e-mail]
    CreedFeed.com - feed your need for Creed!

  2. #2
    SitePoint Member
    Join Date
    Dec 2000
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by HumanClay
    I want to strip the last character in a variable in PHP. HOw do I do this? i.e.

    $var = "words";

    I want to be able to strip that "s" off the end of the word. I forgot how to do this ... Thanks.
    If it's Perl you can use the function chop ;o)

    $var = "words";
    chop($var);
    Adelina.........
    Web Integrator.........

  3. #3
    SitePoint Member
    Join Date
    Dec 2000
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by HumanClay
    I want to strip the last character in a variable in PHP. HOw do I do this? i.e.

    $var = "words";

    I want to be able to strip that "s" off the end of the word. I forgot how to do this ... Thanks.
    I'm sorry, I didn't know what was PHP. After readind all the messages now I know!
    So I guess you'll receive a better answer soon!
    Good luck!
    Adelina.........
    Web Integrator.........

  4. #4
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, too bad I couldn't find it either, but you could use substr() for this:

    substr(string,0,strlen(string)-2);
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would go with what Son said except shouldn't be
    $var = substr($var,0,strlen($var)-1);
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh yeah, Freddy! I thought the substr has syntax like this: substr(str,startIndex,endIndex)
    But it is: substr(str,startIndex,lengthToGrasp);

    So yes, it should be what Freddy gave ya!
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  7. #7
    Rusted & Weathered HumanClay's Avatar
    Join Date
    Sep 2000
    Location
    Milwaukee, WI
    Posts
    225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. It worked.
    Steve Caponetto - [profile] [e-mail]
    CreedFeed.com - feed your need for Creed!

  8. #8
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Alternatively, using regular expressions:

    $var = ereg_replace("^(.*).$","\\1",$var);
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  9. #9
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a note: with reg. expression, the cost is performance if you use it many times
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •