0

I would like to get name from full email address and tried htmlentities and html_entity_decode but special character < and > gives issue to get the name. As full email address is coming from array (key-value) not as a string.

Array value:

Array
(
   [ENV_MAIL] => "Project Bribara<project.bribara@gmail.com";
)

Tried code:

// Project email
$environment['ENV_MAIL'] = "Project Bribara<project.bribara@gmail.com";
$projectEmail = $environment['ENV_MAIL'];
echo $projectEmail ;
$projectName = explode("@", htmlentities($adminEmail));
echo html_entity_decode($projectName);

Expected:

"Project Bribara"

Actual:

"Project Bribara<project.bribara

How can I get the output as per expected?

halfer
  • 19,824
  • 17
  • 99
  • 186
Jitesh Sojitra
  • 3,655
  • 7
  • 27
  • 46

5 Answers5

3

You can do it like this. Here we are using explode() function to split a string.

Try this code snippet here

$environment['ENV_MAIL'] ="Project Bribara<project.bribara@gmail.com";
echo explode("<",$environment['ENV_MAIL'])[0];
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • Your solution is good and upvoted but due htmlentities in the value (which may not be visible) gives issue to explode or substr doesn't work at all. – Jitesh Sojitra Sep 02 '17 at 06:56
2

You can use Substr and strpos.
Substr splits the string from first character to where strpos finds the <.

$str = "Project Bribara<project.bribara@gmail.com";
// Or ^^ is $projectEmail if I understand it correct?

Echo substr($str, 0, strpos($str , "<"));

https://3v4l.org/qSF4h

If the < causes problems maybe you can try regex?

https://regex101.com/r/EfkwLX/1

// Match a-Z and space multiple until non word character
$re = '/([a-zA-Z ]+)\W/';
$str = 'Project Bribara<project.bribara@gmail.com';

preg_match($re, $str, $matches);

Echo $matches[1];  

https://3v4l.org/fuEL5

Andreas
  • 23,610
  • 6
  • 30
  • 62
1

You should split the string into the name and email address using "<" first. And then split with the "@" if you wish.

David Crowe
  • 113
  • 6
-1
// Project email
$environment['ENV_MAIL'] = "Project Bribara<project.bribara@gmail.com";
$projectEmail = $environment['ENV_MAIL'];
echo $projectEmail;
$projectName = html_entity_decode ($projectEmail);
echo $projectName;
Jitesh Sojitra
  • 3,655
  • 7
  • 27
  • 46
-1

There are a few ways that you can tackle this issue: (Demo Link)

The reason that all of the other answers fail is because you have misrepresented your input string. Your actual input string contains &lt; which is the html entity for <. My methods below will clarify this point to future SO readers and help you to select the best approach for your project.

$environment['ENV_MAIL']="Project Bribara&lt;project.bribara@gmail.com";
//                                       ^^^^ html entity causing you trouble

$decoded_string=html_entity_decode($environment['ENV_MAIL']);  // convert "&lt;" to "<"
var_export(explode('<',$decoded_string));  // generate an array of the two parts for future access

echo "\n\n";

echo "Project Name = ",strstr($decoded_string,'<',true);  // use strstr on < of decoded string with "true" param

echo "\n\n";

echo "Project Email = ",substr($decoded_string,strpos($decoded_string,'<')+1);  // use substr&strpos on <

echo "\n\n";

// or less potentially damaging to your input string...
var_export(explode('&lt;',$environment['ENV_MAIL']));

Output:

array (
  0 => 'Project Bribara',
  1 => 'project.bribara@gmail.com',
)

Project Name = Project Bribara

Project Email = project.bribara@gmail.com

array (
  0 => 'Project Bribara',
  1 => 'project.bribara@gmail.com',
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136