Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple Question: Trimming Strings (not ltrim or rtrim or trim)

    G'day guys,

    Wondering if you could help me out with this (no doubt easy one):

    What I want to do is trim a string by x number of characters either from the left or right as desired:

    Example:

    For the string:
    PHP Code:
    $string "2007-01-05 12:20:00"
    I want to remove the last ' 12:20:00' bit which is 9 characters long.
    now the way I have managed this so far is to explode the string into an array, then echoing the first part of the array:

    PHP Code:
    $string "2007-01-05 12:20:00";
    $string explode(" "$string);
    echo 
    $string[0]; 
    But now as I want to also remove the year '2007-' which is the frist 5 characters of the string - now an array - I figure instead of doing another explode by '-' and calling the [1] and [2] part of the array surely I can just trim off x amount of characters.... I could with ASP.... Please enlighten me. I looked at the trims (l,r,) but they only remove whitespace and a chosen character... or am I wrong....

    Thanks in advance!
    Last edited by neillglobal; 03-01-2007 at 03:24 AM. Reason: Post not finished.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    Do it all at once with a single statement - http://www.php.net/manual/en/function.substr.php
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    In your case, you'd use it like this:

    $string = "2007-01-05 12:20:00";
    $date = substr($string, 5, 5); // string starts at 0, since you want character 6 you get 6 - 1= 5, and you want the 01-05 which is 5 characters long

  • #4
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great! Brilliant thanks.... the learning never stops


  •  

    Posting Permissions

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