Power of PHP: Add hyperlinks to user's own text

Last time we included a class to add hyperlinks to our text, but we just added the text as a local variable. This time we're going to let the user input text and we'll add the hyperlinks for them. For this we need to add some HTML. How do we do this? Simple just add regular HTML tags to the beginning and end of the PHP file. (note: we still save the file with the extension .php despite including HTML)

The HTML means we can create a form, and that form is going to look like this:
 
<html>
<form action='UserTextAddHyperLinks.php' method='post'>
<textarea name='hypertext' rows=5> </textarea>
<input type='submit' value='add hyperlinks' />
</form>

</html>
The form action points to the same file as we are adding code to. There is no separate file, so we save our file as UserTextAddHyperLinks.php

Next we need to write the PHP code, which looks almost exactly the same as last time but with one difference. The $text variable isn't defined in place with a string of text. Instead it looks like this:
 
$text = $_POST['hypertext'];
So putting the entire file together it looks like this:
 
<html>
<form action='UserTextAddHyperLinks.php' method='post'>
<textarea name='hypertext' rows=5> </textarea>
<input type='submit' value='add hyperlinks' />
</form>

<?php
include ("classes/livetext.php" );

$text = $_POST['hypertext'];

$textwithlinks=new liveText(); 
$textwithlinks->addHyperlinksToURLs($text);
?>
</html>


You will need to have saved in a subfolder called "classes" your "livetext.php" file from last time to make this work, but that's it. Cut and paste some text in that includes URLs and they will have the hyperlinks added.

Next time we'll further refine this.

Comments