Exercise on String / Array Functions:
irene@example.com, felix@example.com, tony@example.com
- How can I split the string above to be a list of emails, and display them in a <ul><li> list set?
- How can I get the name from the emails above and display them on a list?
- Get the email domain from the string above.
- replace the domain name to "hotmail" and output the emails in a list.
- Join "lynx@aims.com" and "may@aims.com" to be the last items of the list of emails above.
echo "<b>Question Number 4 : change example to hotmail</u></b><br/>";
ReplyDeleteecho "irene@example.com<br/>felix@example.com<br/>tony@example.com <br/><br/>";
echo "<b><u>Answer Number 4 :</u></b><br/>";
$email = array('<li>irene@example.com<br/>','<li>felix@example.com <br/>','<li>tony@example.com <br/></li>');
$search ="example";
$replace ="hotmail";
foreach ($email as $item)
{
echo str_replace ($search, $replace, $item);
}
welldone, though the 3 emails supposed to be a string instead of arrays.
Delete$string = "irene@example.com, felix@example.com, tony@example.com";
ReplyDelete$split = explode(", ", $string );
echo "<ul>";
foreach($split as $item){
echo "<li>$item</li>";
}
echo "</ul>";
good job
DeleteAnswer Question 3
ReplyDelete$arr=array('irene@example.com','felix@example.com',
'tony@example.com');
foreach($arr as $item)
{
echo $substringfound = strstr ($item,'ex');
echo "\n";
}
good. but 'ex' not a practical $needle to search a domain. How about split each emails to be 2 part, and u get the 2nd part of string as domain name, and '@' as the delimiter?
Delete<?php
ReplyDelete$string=array("irene@example.com","felix@example.com","tony@example.com");
array_push($string,"lynx@aims.com","may@aims.com");
echo"<ul>";
foreach($string as $item){
echo"<li>$item</li>";
}
echo"</ul>";
?>
great. but you skip the step changing string to array.
Delete<?php
ReplyDelete$email = "irene@example.com, felix@example.com, tony@example.com";
$split = explode(", ", $email );
echo "<ul>";
foreach($split as $item){
$domain = strstr ($item, "@",2);
echo "<li>$domain</li>";
}
echo "</ul>";
?
good. strstr ($item, "@",2) the 3rd parameter should be a bool type (1 or 0), choose 1 to get the string before the needle.
Delete