Re: split string value again

From: Date: Thu, 29 Mar 2001 23:28:26 +0000
Subject: Re: split string value again
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-46184@lists.php.net to get a copy of this message
"Jacky" <jacky@activelifestyle.com> wrote: > I have a vairable that stores email address value. I need to break it so > that I will only get the dmain name bit to store in another variable so > that > I can redirect user to that domain, like if user email is test@foo.com. > then I would like to break that and take only foo.com bit to use for > navigation. I don't think any of the responses you got took into account email addresses like user@host.domain.tld. If you truely just want domain.tld it's a little more complex. I happened to have written a script a few months ago that does what you want. Even if you don't need something this complex in your case, my code uses 5 or 6 string functions and arrays so it's a pretty good example of how to manipulate strings in PHP. <?php // Expecting a properly formatted email address. $string_in = 'steve@mail.richmond.va.us'; // strpos() finds position of '@' character. // substr() returns portion of email address to the // right of '@' (the host). $string_tmp = substr( $string_in, strpos( $string, '@' ) + 1 ); // substr_count() returns the # of occurrences of '.' within the host. $dot_count = substr_count( $string_tmp, '.' ); // If only one dot was found, we're done. if ( $dot_count == 1 ) { $string_out = $string_tmp; } // If more than one dot was found, there's more to do. else { // explode() splits the host into elements of an array. // The elements are the portions of the string between the dots. $parts = explode( '.', $string_tmp ); // count() returns the number of elements. We want the // last 2 elements. Since the array elements begin at 0, not 1, // we need to get elements n-2 and n-1, not n-1 and n, which // might normally be expected. $string_out = $parts[count( $parts ) - 2] . '.' . $parts[count( $parts ) - 1]; } // print the desired output. echo $string_out; ?> -- Steve Werby President Befriend Internet Services LLC Tel: 804-355-WEBS http://www.befriend.com/

« previous php.general (#46184) next »