Results 1 to 10 of 10
  1. #1

    Another Quickie - Editing a String

    Ok a quickie. I have a string of emails in the format:

    [email protected],[email protected],[email protected],[email protected],

    (note there is a trailing comma). Anyway, if this is stored in a string, say $email... what would I use to delete one of the email addresses from the middle, say [email protected]. So the final string would be:

    [email protected],[email protected],[email protected],

    Thanks.

  2. #2
    Join Date
    May 2004
    Location
    Singapore
    Posts
    262
    One way is to create an array (or some similiar construct) of email addresses from the string of email addresses.
    Search this array for the email address, and remove (or otherwise note) it.
    Then re-construct the string from the modified array.

    Another way is to search for the position of the email address in the string.
    Once you know the start and end point, you just copy everything else into a new string.
    #include<cstdio>
    char*s="#include<cstdio>%cchar*s=%c%s%c;%cint main(){std::printf(s,10,34,s,34,10);}";
    int main(){std::printf(s,10,34,s,34,10);}

  3. #3
    Join Date
    Jan 2005
    Posts
    45
    Try:
    PHP Code:
    $list=explode(",",$email);
    $j=0;
    for(
    $i=0;$i<count($list)-1;$i++){
    if(
    $list[$i]!=$needle){$newlist[$j]=$list[$i];$j++;}
    }
    $email=implode(",",$newlist); 

  4. #4

  5. #5

  6. #6
    Just an alternative:

    PHP Code:
    $email_string [email protected],[email protected],[email protected],[email protected],';

    $email_replace [email protected]';

    $email_string preg_replace ("/$email_replace,/"''$email_string);

    print 
    $email_string
    Adnan.
    FusionRays.com
    Shared and Reseller Plans

  7. #7
    Join Date
    May 2004
    Location
    Singapore
    Posts
    262
    Actually, an improvement to intransit's code would be:
    PHP Code:
    $haystack explode(","$email);
    if ((
    $key array_search($needle$haystack)) !== false) {
        unset(
    $haystack[$key]);
        
    $email implode(","$haystack);

    This is basically:
    One way is to create an array (or some similiar construct) of email addresses from the string of email addresses.
    Search this array for the email address, and remove (or otherwise note) it.
    Then re-construct the string from the modified array.

    intransit's method takes the unnecessary step of creating a new array first, and then re-constructing the string from the new array.
    #include<cstdio>
    char*s="#include<cstdio>%cchar*s=%c%s%c;%cint main(){std::printf(s,10,34,s,34,10);}";
    int main(){std::printf(s,10,34,s,34,10);}

  8. #8
    Join Date
    May 2005
    Location
    Scranton, PA
    Posts
    21
    it is a string, use string replacement or regex functions.

    creating an array and looping thru it seems like an overkill, imo.
    Senior Software Developer
    BurstNET® - The Speed the Internet Travels™
    BurstNET® - an INC500™ Company
    http://www.burst.net - http://www.nocster.net

  9. #9
    Join Date
    May 2004
    Location
    Singapore
    Posts
    262
    Well, the CSV-like format of the string makes it easy to work with explode/implode.
    There could be a gotcha in using the 2nd method with str_replace() (preg_replace() would also be overkill in this case) if for some reason the last item did not have a trailing comma as stipulated by Jeanco.
    #include<cstdio>
    char*s="#include<cstdio>%cchar*s=%c%s%c;%cint main(){std::printf(s,10,34,s,34,10);}";
    int main(){std::printf(s,10,34,s,34,10);}

  10. #10
    thanks for the informative thread guys. Very interesting.

Posting Permissions

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