Well, to reserve all tags, you'd simply NOT use strip_tags. If you'd like to have a set of allowable HTML tags, the easiest thing to do is to parse out all the tags that you want to keep and strip the rest. Alternatively, you can implement a similar system to the "forum code" that you see here, where people use HTML-esque tags using square brackets instead of angle brackets. For example, [brace]b]bold words[brace]/b] instead of <b>bold words</b>.
In that case, you can simply strip ALL html tags, and use preg_replace() to replace the accepted forum code tags with HTML tags.
Converting URLs to links is also just a matter of regular expression search and replace.
There are lots of examples for doing these types of conversions online. Try looking at the function preg_replace() for an idea of where to start.
http://www.php.net/preg_replace
Another method would be to use the preg_replace_callback() function to manage all your HTML links. A callback function is a function that gets called when some event happens. In preg_replace_callback, your function is called whenever a pattern is matched. The callback function takes the place of the replacement pattern. It's only parameter is the array of strings that matched your pattern.
For example, suppose I had this callback function:
function linkify($matches)
{
return "<a href=\"{$matches[0]}\">{$matches[0]}</a>";
}
This function clearly expects the entire pattern to match a URL. We can use this to create a hyperlink out of a regularly-typed URL.
Here's a sample program for you to play with:
<?php
function linkify($matches)
{
return "<a href=\"{$matches[0]}\">{$matches[0]}</a>";
}
$pattern = '!\bhttps?://([\w\-]+\.)+[a-zA-Z]{2,3}(/(\S+)?)?\b!';
/* Pattern above matches any link starting with http:// or
https://,
* followed by one or more groups of at least one word-character (\w)
* and a period (\.), followed by either two or three letters ([a-zA-Z]),
* followed by an optional slash, and any number of optional non-whitespace
* characters.
* The \b assertions force the pattern to begin and end at a "word boundary",
*
*
* This is by no means a "complete" URL matching pattern, but it's short and
* matches lots of URLs, including:
*
http://www.bigaction.org
*
http://p2p.wrox.com/topic.asp?TOPIC_ID=5482
*
https://login.yahoo.com/config/login
*
*/
$textarea = isset($_POST['some_text'])? $_POST['some_text'] : "
Feel free to type some sample text here, including some links.
For example:
http://www.bigaction.org
http://www.bigaction.org/
https://login.yahoo.com/config/login
http://p2p.wrox.com/topic.asp?TOPIC_ID=5482
";
echo <<<EOF
<form method="post" action="{$_SERVER['PHP_SELF']}">
<textarea name="some_text" cols="60" rows="8">{$textarea}</textarea>
<br />
<input type="submit" name="submit_button" value="Linkify!" />
</form>
EOF;
if (isset($_POST['some_text']))
{
echo "\n";
echo "<pre>\n";
echo preg_replace_callback($pattern, 'linkify', $_POST['some_text']);
echo "</pre>\n";
}
?>
Take care,
Nik
http://www.bigaction.org/