Search code examples
phphtmlemaildelimitertablecell

How to split an email on @ symbol and display in two table cells?


I want to display the values separately which I got through explode(). But the problem is that my table shows me two values which are exactly same. I don't know the reason behind it and thoroughly searched on the StackOverflow, but couldn't find my answer.

Code:

<!DOCTYPE html>
<html>
    <head>
        <title>PHP3</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <link rel="http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    <body>
        <div class="container">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>User Name</th>
                        <th>Domain Name</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $str = "[email protected]";
                    $hello = explode("@",$str);
                    foreach ($hello as $value)
                    {
                        echo "<tr>";
                            echo "<td>$hello[0]</td>";
                            echo "<td>$hello[1]</td>";
                        echo "</tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </body>
</html>

Solution

  • echo "<tr>";
    foreach ($hello as $value)
    {
            echo "<td>$value</td>";
    }
    echo "</tr>";
    

    Basically you need to set TR tag before the foreach.

    Secondly in foreach, the $value contains both values in iterations so do not need to print $hello[0].

    Just print $value