"Jacky" <[EMAIL PROTECTED]> 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 [EMAIL PROTECTED]
> 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 [EMAIL PROTECTED]  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 = '[EMAIL PROTECTED]';

// 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/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to