Advanced Search

Results 1 to 5 of 5

Thread: trim() www. taking another w

  1. #1
    Join Date
    Aug 2006
    Location
    North Las Vegas, Nevada
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default trim() www. taking another w

    For the life of me, I can't understand why trim() takes off more than I'm asking for. All I want removed is www. from a string, but in the event of www.w, it takes that off too. This is what I have:

    PHP Code:
    trim('www.wt''www.'); 
    This results in t


    PHP Code:
    trim('www.rwt''www.'); 
    This results in rwt

    I've since just started using substr to take out the first four, but it will absolutely keep me up at night not knowing why it keeps taking that extra w.
    Last edited by SaishuHane; 02-20-2013 at 12:23 AM. Reason: Resolved

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,681
    Thanks
    78
    Thanked 89 Times in 87 Posts

    Default

    The portion to be removed is the character list not a string. For example

    Code:
    trim('www.wt', 'www.');
    is the same as
    Code:
    trim('www.wt', 'w.');
    In regards to your second example remember that trim will trim the selected characters from the beginning and end of your string. If a letter is removed from the end or beginning of your string and the next character is also part of the character list then it too will be removed. Since the "w" is between an "r" and a "t" and neither the "r" or the "t" are part of the character list. That "w" will not be removed. It will not be removed because at no point is it located at the beginning or end of the string.

    Does that help explain it?
    To choose the lesser of two evils is still to choose evil. My personal site

  3. The Following User Says Thank You to james438 For This Useful Post:

    SaishuHane (02-20-2013)

  4. #3
    Join Date
    Aug 2006
    Location
    North Las Vegas, Nevada
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thank you much! I didn't quite get it at first, but after testing some things based on what you said, I'm not going to be losing any sleep tonight!

  5. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,681
    Thanks
    78
    Thanked 89 Times in 87 Posts

    Default

    Happy to help
    To choose the lesser of two evils is still to choose evil. My personal site

  6. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    261
    Thanked 690 Times in 678 Posts

    Default

    Did you work this out? You can do this, but it will need to be done in a slightly different way.

    One option would be the following:
    PHP Code:
    $str str_replace('www.','%',$str);
    trim($str,'%');
    $str str_replace('%','www.',$str); 
    Just make sure you don't have any other '%' characters in your string (or use something else as the placeholder).


    However, there may be a better way to reach your overall goal. That's just if you specifically want to use trim().
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Similar Threads

  1. MIMEdecoder trim()
    By 20pictures in forum PHP
    Replies: 0
    Last Post: 08-05-2009, 05:18 PM
  2. Taking away the links
    By djohno24 in forum Looking for such a script or service
    Replies: 2
    Last Post: 11-17-2008, 11:36 AM
  3. php (trim and join jpgs) : (plot data)
    By Strangeplant in forum PHP
    Replies: 2
    Last Post: 02-01-2007, 01:05 PM
  4. Taking snapshot of my web page
    By rajug in forum JavaScript
    Replies: 6
    Last Post: 01-05-2007, 12:12 PM
  5. ... taking XHTML seriously ...
    By esteban in forum HTML
    Replies: 6
    Last Post: 05-05-2006, 07:35 AM

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
  •