III. Using NBBC

[ Previous: J. Limited-Length and Plain-Output Modes | Next: L. Styles, Colors, and CSS Considerations ]

K. URL Autodetection

It can be useful to have NBBC detect URLs (web addresses) and e-mail addresses and turn them into links. For example, consider the following text:

User Input:
Go to www.google.com for all your searching needs!
Output:
Go to www.google.com for all your searching needs!

It would be nice if the "www.google.com" there was clickable, and NBBC includes a feature for making it just that. With URL autodetection enabled, anything that looks like part of a web address, a URL, or an e-mail address will be turned into a link, like this:

Code:
$bbcode = new BBCode; $bbcode->SetDetectURLs(true); $output = $bbcode->Parse($input);
User Input:
Go to www.google.com or example.com:8086 or http://www.google.com or my friend Larry, larry@example.com, for all your searching needs!
Output:
Go to www.google.com or example.com:8086 or http://www.google.com or my friend Larry, larry@example.com, for all your searching needs!

URL autodetection picks out domain names (like "www.google.com"), URLs that use the HTTP, HTTPS, or FTP protocols (like "http://www.google.com"), and e-mail addresses (like "larry@example.com") and turns them into links. It uses a template system just like BBCODE_MODE_ENHANCED does to transform them, so you can even make any of the results below possible:

Code:
$bbcode = new BBCode; $bbcode->SetDetectURLs(true); $bbcode->SetURLPattern('<a href="{$url/h}">{$text/h}</a>'); $output = $bbcode->Parse($input);
Output:
Go to www.google.com for all your searching needs!
Code:
$bbcode = new BBCode; $bbcode->SetDetectURLs(true); $bbcode->SetURLPattern('{$text/h} <a href="{$url/h}">[link]</a>'); $output = $bbcode->Parse($input);
Output:
Go to www.google.com [link] for all your searching needs!
Code:
$bbcode = new BBCode; $bbcode->SetDetectURLs(true); $bbcode->SetURLPattern('<a href="{$url/h}">{$text/h} <img src="external.gif" width="11" height="11" alt="External Link" /></a>'); $output = $bbcode->Parse($input);
Output:
Go to www.google.com External Link for all your searching needs!

For additional documentation on templating, see the FillTemplate() function, which is shared by all of the template code in NBBC and can be used in your own programs as well; and also see the list of template flags.

[ Previous: J. Limited-Length and Plain-Output Modes | Next: L. Styles, Colors, and CSS Considerations ]


Copyright © 2010, the Phantom Inker. All rights reserved.