Search This Blog

Thursday, June 28, 2012

Day 14 Exe PHP: String, Array

Exercise on String / Array Functions:

 irene@example.com, felix@example.com, tony@example.com
  1. How can I split the string above to be a list of emails, and display them in a <ul><li> list set?
  2. How can I get the name from the emails above and display them on a list? 
  3. Get the email domain from the string above.
  4. replace the domain name to "hotmail" and output the emails in a list.
  5. Join "lynx@aims.com" and "may@aims.com" to be the last items of the list of emails above.

10 comments:

  1. echo "<b>Question Number 4 : change example to hotmail</u></b><br/>";
    echo "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);
    }

    ReplyDelete
    Replies
    1. welldone, though the 3 emails supposed to be a string instead of arrays.

      Delete
  2. $string = "irene@example.com, felix@example.com, tony@example.com";

    $split = explode(", ", $string );

    echo "<ul>";
    foreach($split as $item){
    echo "<li>$item</li>";
    }
    echo "</ul>";

    ReplyDelete
  3. Answer Question 3

    $arr=array('irene@example.com','felix@example.com',
    'tony@example.com');

    foreach($arr as $item)
    {
    echo $substringfound = strstr ($item,'ex');
    echo "\n";
    }

    ReplyDelete
    Replies
    1. 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
  4. <?php
    $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>";
    ?&gt

    ReplyDelete
    Replies
    1. great. but you skip the step changing string to array.

      Delete
  5. <?php
    $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>";

    ?

    ReplyDelete
    Replies
    1. 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