1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4. <head>
5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
6. <title>Make URL Click-able</title>
7. </head>
8. <body>
9. <?php // Script 13.2 - convert_url.php
10. /* This script turns a valid URL into an HTML link. */
11.
12. if ( isset($_POST['submitted'])) { // Has the form been submitted?
13.
14. // Trim off extraneous spaces, just in case:
15. $url = trim($_POST['url']);
16.
17. // Establish the patterns:
18. $pattern1 = '^((http|https|ftp)://){1}([[:alnum:]-])+(\.)([[:alnum:]-]){2,6}([[:alnum:]/+=%&_.~?-]*)$';
19. $pattern2 = '^([[:alnum:]-])+(\.)([[:alnum:]-]){2,6}([[:alnum:]/+=%&_.~?-]*)$';
20.
21. // Test the submitted value against the patterns....
22. if (eregi($pattern1, $url)) { // Check for an existing http/https/ftp.
23.
24. $url = eregi_replace($pattern1, '<a href="\\0">\\0</a>', $url);
25. print "<p>Here is the URL: $url<br />The code is now: " . htmlentities ($url) . '</p>';
26.
27. } elseif (eregi($pattern2, $url)) { // No http/https/ftp, add http://.
28.
29. $url = eregi_replace($pattern2, '<a href="http://\\0">\\0</a>', $url);
30. print "<p>Here is the URL: $url<br />The code is now: " . htmlentities ($url) . '</p>';
31.
32. } else { // Invalid URL.
33. print'<p>Please enter a valid URL.</p>';
34. }
35.
36. } // End of main conditional.
37. // Display the HTML form:
38. ?>
39. <form action="convert_url.php" method="post">
40. <p>URL: <input type="text" name="url" size="30" /></p>
41. <input type="submit" name="submit" value="Convert" />
42. <input type="hidden" name="submitted" value="true" />
43. </form>
44. </body>
45. </html>