Skip to content
PHP Logo

How can PHP be utilized to dynamically generate HTML content, such as linking email addresses in a guestbook, based on user input?

To dynamically generate HTML content like linking email addresses in a guestbook based on user input, you can use PHP to parse the user input for email addresses and then generate HTML links for each email address found.

php
<?php
// Sample user input containing email addresses
$user_input = "Hello, please contact me at user@example.com or info@test.com for more information.";

// Use regular expression to find email addresses in the user input
preg_match_all('/[\w\.-]+@[a-z\.-]+\.[a-z]+/', $user_input, $matches);

// Loop through the matched email addresses and generate HTML links
foreach ($matches[0] as $email) {
    echo '<a href="mailto:' . $email . '">' . $email . '</a><br>';
}
?>