Question

please check the following:

$email  = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com

Is there a way I can return @example without the .com?

So, basically I need to search a string for a set of characters which i DON'T know, which exist between a set of charaters I DO know.

So, in the following I want to return "dog":

$string = "I used to have a pet dog named spot";

I can get the last section of the string using:

$pet_type = strstr($string, 'pet ');

Is this possible?

Was it helpful?

Solution

or use 2 times explode

$email  = 'name@example.com';
        $email  = explode( '@', $email);
        $email = explode('.', $email[1]);

        $email = $email[0];
        echo $email;

returns: example

OTHER TIPS

You answer the question yourself in the tags. Using regex/preg_match you can look for patterns and extract values that match.

Here's an introduction on regular expressions, it can look quite daunting but don't be sacred, it's quite logical :)

For your example with the domain, you could do something like this:

preg_match("@[^\.]+", $email, $matches);
$domain = $matches[0];

Try substr() combined with strpos()

$start = strpos($email, '@');
$length = strpos($email, '.com') - $start;
$domain = substr($email, $start, $length);
$email = 'name@host.com';
$at_pos = strpos($email, '@');
$dot_pos = strpos($email,'.');
$domain_length = $dot_pos - $at_pos;
$domain = substr($email,$at_pos,$domain_length);
echo $domain;

I use this a lot because it makes regular exprasions a lot simpler. you can add ^ to a a range to invert it. [^aeiou] is th range of ALL characters but a, e, i, o und u.

For your example try

@[^\.]+

This will match "@example".

You need regular expressions.

preg_match('/@(\w+)\.com/', $email, $match);
$domain = $match[1];

See : http://www.php.net/manual/fr/book.pcre.php

Basically, in the preg_match() call, I'm testing if the string $email has the following sequence : an at symbol, then any positive number of word characters captured in a group using parentheses ((\w+)), then a dot (escaped : \.) and finally, com. If the match succeeds, the capture groups are put in the $match array and may be retrieved using their left-to-right position index.

The matching pattern can be improved depending on the type of characters you are expecting in your input. Overall REGEXes are slower than simple string replacements but much, much more powerful. It's worth learning how they work.

preg_match('/\@([a-zA-Z0-9-]+)?/', 'test@site-test.com', $matches);

Array ( [0] => @site [1] => site )

$matches[0] is what you want to use.

You could try:

$str = '';
$email  = 'name@example.com';
$domain = strstr($email, '@');
$domain = str_split($domain);
foreach($domain as $split){
$str .= $split;
if($split == '.'){
break;
}
}
echo $str; // prints @example

or:

$email  = 'name@example.com';
$email = strstr($email, '@');
$email = explode(".", $domain);
echo $email[0]; // prints @example
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top