Results 1 to 5 of 5

Thread: how to check if a ";" is at the end of a string?

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Location
    www.mechboxer.de
    Posts
    15

    how to check if a ";" is at the end of a string?

    hi!
    I have got a problem:
    how can I check if a ";" is at the end of a string?

    e.g.
    $str = "This is a test;";

    now I want the php script to check if at the end of $str is a ";" - if so, there should come an echo "found a ";" at the end"; !

    perhaps you can help me, by saying how the function is called.

    thanx a lot,

    stylewriter
    -=|www.mechboxer.de|=-

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Code:
    $str = "blah blah;";
    
    if (substr($str, strlen($str) - 1, 1) == ";") {
        echo "found ;!";
    }
    Should work.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Location
    www.mechboxer.de
    Posts
    15
    ok, thanx!
    and how is it possible that the ";" at the end of the string will be deleted?
    sry, but I don't know the names of the functions allready...
    Last edited by Stylewriter; Jan 19th, 2003 at 01:02 PM.
    -=|www.mechboxer.de|=-

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Code:
    $str = "blah blah;";
    
    if (substr($str, strlen($str) - 1, 1) == ";") {
        //if the ; exists, remove it:
        $str = substr($str, 0, strlen($str) - 1);
    }
    
    echo $str; //output: blah blah
    Function substr():
    Syntax: substr (string string, int start [, int length])
    Description: Substr returns the portion of string specified by the start and length parameters.

    Example:
    Code:
    echo substr("12345", 1, 2);
    //output: 23
    Function strlen():
    Syntax: strlen (string str)
    Description: Returns the length of string.

    Example:
    Code:
    echo strlen("1234");
    //output: 4
    Hope this helps.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Location
    www.mechboxer.de
    Posts
    15
    yes, it works!
    thanx a lot for the detailed information!!!
    -=|www.mechboxer.de|=-

Posting Permissions

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



Click Here to Expand Forum to Full Width