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
    Regular Coder
    Join Date
    Sep 2002
    Location
    Saskatoon SK Canada
    Posts
    174
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Delete string after comma

    How would I truncate a string after a certain character? I don't know the index of that character.

    I think this is close, but I need to replace the 18 with the index of the comma in the variable mystring since I won't know where the user put in a comma.

    $mystring = "Hello I'm a string, and I have a comma.";
    $mystring = substr_replace($mystring , '', 18, -1);

    my result of echoling $mystring would be:
    "Hello I'm a string"
    Colin Puttick

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,511
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    $mystring = "Hello I'm a string, and I have a comma.";
    list ($before,$after) = split (',', $mystring);
    echo $before;

    I'm not sure if you have to escape the comma
    in the split command or not ( '\,' )

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Location
    Saskatoon SK Canada
    Posts
    174
    Thanks
    2
    Thanked 0 Times in 0 Posts
    My server didn't require escaping the comma. Thanks! My brain works in Javascript and PHP doesn't always translate.

    PHP Code:
        $has_comma = (stristr($your_email",")>-1) ? 0;
        if (
    $has_comma) {
            list (
    $before,$after) = split (','$your_email);
            
    $your_email $before;
        } 
    Colin Puttick

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    PHP Code:
    if (($pos strpos($str',')) !== false)
    {
       
    $str substr($str0$pos);

    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

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