Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2006-08-09 01:40:59
- Ace of Dubs
- Member

- Registered: 2006-04-17
- Posts: 446
Parsing URLs in Comments
I have seen this done in a lot of forum software. Is there an easy way to get any instance of “http://” within my comments whipped up into an actual link? Textile works just fine, but very few of the users actually use them and there are far more people slapping up inactive text as links.
Offline
#2 2006-08-09 02:24:37
- Mary
- Sock Enthusiast
- Registered: 2004-06-27
- Posts: 6,236
Re: Parsing URLs in Comments
Sure, make yourself a plugin and use a regular expression (Google) or something like this.
Offline
#3 2006-08-09 03:13:33
- Ace of Dubs
- Member

- Registered: 2006-04-17
- Posts: 446
Re: Parsing URLs in Comments
I just tried this out with zem’s template. Your linked example is exactly what I was looking for, I just couldnt get my head around how to fit $atts and $thing into the picture, PHP noob that I am :(
Offline
#4 2006-08-09 04:25:35
- Mary
- Sock Enthusiast
- Registered: 2004-06-27
- Posts: 6,236
Re: Parsing URLs in Comments
Still having problems? What have you got at the moment? Oh and, have you looked at Alex’s tutorials? There’s a whole whack of plugin resources available.
Last edited by Mary (2006-08-09 04:26:40)
Offline
#5 2006-08-09 11:54:33
- Ace of Dubs
- Member

- Registered: 2006-04-17
- Posts: 446
Re: Parsing URLs in Comments
Thanks Mary, I been pouring over those pages, but realize I still have a ways to go in my understanding of PHP.
My first attempt yields this reaction:
bc..
(Parse error: syntax error, unexpected ‘<’ in /usr/home/housecaf/public_html/123txp/textpattern/lib/txplib_misc.php(459) : eval()’d code on line 1
The above errors were caused by the plugin:ace_easylinks)
I always hated these type of errors that point to obscure lines in the txp core instead of the offending plugin… I dont even know where to start! Anyway, here is my humble code… Feel free to rip it apart
bc..
<?php
}
function ace_easylinks($atts, $thing) {
global $thisarticle;
$text = preg_replace(“/(\r\n|\n|\r)/”, “\n”, $text);
$lines = explode(“\n”, $text);
for ($x = 0, $y = count($lines); $x < $y; $x++) {
$line = $lines[$x];
$words = explode(’ ‘, $line);
for ($i = 0, $j = count($words); $i < $j; $i++) {
$word = $words[$i];
$punctuation = ‘.,\’”)(<>;:’; // Links may not end in these
if (substr($word, 0, 7) 'http://' ||
substr($word, 0, 4) ‘www.’) {
$trailing = ‘’;
// Knock off ending punctuation
$last = substr($word, -1);
while (strpos($punctuation, $last) !== false) {
// Last character is punctuation – eliminate it
$trailing .= $last;
$word = substr($word, 0, -1);
$last = substr($word, -1);
}
// Make link, add trailing punctuation back afterwards
$link = $word;
if (substr($link, 0, 4) == ‘www.’) {
// This link needs an http://
$link = ‘http://’.$link;
}
$word = ‘<a href=”’.$link.’”>’.$word.’</a>’.$trailing;
}
$words[$i] = $word;
}
$lines[$x] = implode(’ ‘, $words);
}
return implode(“\n”, $lines);
$out[] = parse($thing)
}
?>
Offline
#6 2006-08-09 15:36:08
- Mary
- Sock Enthusiast
- Registered: 2004-06-27
- Posts: 6,236
Re: Parsing URLs in Comments
Yeah, you got a couple problems there.
- Make sure you’re using the plugin template properly: you don’t put in open and closing php tags. Just paste all your function inbetween
# --- BEGIN PLUGIN CODE ---and# --- END PLUGIN CODE ---. - I recommend placing the conversion script in it’s own function, so you can easily update it at any time
function ace_easylinks($atts, $thing)
{
$out = ace_easylinks_convert($thing);
return parse($out);
}
// -------------------------------------------------------------
// Simon Incutio
// http://simon.incutio.com/code/php/ConvertLinksDemo.php
function ace_easylinks_convert($text)
{
$text = preg_replace("/(\r\n|\n|\r)/", "\n", $text);
$lines = explode("\n", $text);
for ($x = 0, $y = count($lines); $x < $y; $x++)
{
$line = $lines[$x];
$words = explode(' ', $line);
for ($i = 0, $j = count($words); $i < $j; $i++)
{
$word = $words[$i];
// Links may not end in these
$punctuation = '.,\'")(<>;:';
if (substr($word, 0, 7) <2> 'www.')
{
$trailing = '';
// Knock off ending punctuation
$last = substr($word, -1);
while (strpos($punctuation, $last) !== false)
{
// Last character is punctuation - eliminate it
$trailing .= $last;
$word = substr($word, 0, -1);
$last = substr($word, -1);
}
// Make link, add trailing punctuation back afterwards
$link = $word;
if (substr($link, 0, 4) == 'www.')
{
// This link needs an http://
$link = 'http://'.$link;
}
$word = '<a href="'.$link.'">'.$word.'</a>'.$trailing;
}
$words[$i] = $word;
}
$lines[$x] = implode(' ', $words);
}
return implode("\n", $lines);
}
Make sense?
Offline
#7 2006-08-09 16:09:32
- Ace of Dubs
- Member

- Registered: 2006-04-17
- Posts: 446
Re: Parsing URLs in Comments
Thats pretty smooth Mary. I didnt even think to write a separate function…keeps things clean.
Unfortunately the above returns this:
bc..
Parse error: syntax error, unexpected ‘>’ in /usr/home/housecaf/public_html/123txp/textpattern/lib/txplib_misc.php(459) : eval()’d code on line 29
The above errors were caused by the plugin:ace_easylinks
Fatal error: Call to undefined function ace_easylinks_convert() in /usr/home/housecaf/public_html/123txp/textpattern/lib/txplib_misc.php(459) : eval()’d code on line 3
I suspect that this is the function’s fault .. gonna google a few more and see if I make any progress
cheers
Edit: Dumb question.. I notice that most plugins and taghandlers have extract($atts) along with some global variables… could that be the reason this doesnt work?
Last edited by Ace of Dubs (2006-08-09 16:29:50)
Offline
#8 2006-08-10 10:58:45
- Ace of Dubs
- Member

- Registered: 2006-04-17
- Posts: 446
Re: Parsing URLs in Comments
Well I have tried several functions and all of them give errors except for this one, same author as above except this version has no regexes.
bc..
function ace_easylinks($atts, $thing)
{
$out = convertLinks($thing);
return parse($out);
}
// ——————————————————————————————-
// conversion script by Simon Willison
// http://simon.incutio.com/archive/2003/10/19/convertingLinks
function convertLinks($text) {
$words = explode(’ ‘, $text);
for ($i = 0, $j = count($words); $i < $j; $i++) {
$word = $words[$i];
$punctuation = ‘.,\’”)(<>;:’; // Links may not end in these
if (substr($word, 0, 7) 'http://' ||
substr($word, 0, 4) ‘www.’) {
$trailing = ‘’;
// Knock off ending punctuation
$last = substr($word, -1);
if (strpos($punctuation, $last) !== false) {
// Last character is punctuation – eliminate it
$trailing .= $last;
$word = substr($word, 0, -1);
}
// Make link, add trailing punctuation back afterwards
$link = $word;
if (substr($link, 0, 4) == ‘www.’) {
// This link needs an http://
$link = ‘http://’.$link;
}
$word = ‘<a href=”’.$link.’”>’.$word.’</a>’.$trailing;
}
$words[$i] = $word;
}
return implode(’ ‘, $words);
}
The only problem is that now I get no output. I wrapped body and excerpt tags with <code><txp:ace_easylinks></txp:ace_easylinks></code> and now both body and excerpt vanish completely.
Weeeeird!
Offline
Re: Parsing URLs in Comments
I don’t know, if you the script is working properly, but if you want to use your plugin this way <txp:ace_easylinks><txp:body /></txp:ace_easylinks> you need to parse $thing before you pass it to your function. Otherwise the function tries to find a link in this word: <txp:body /> (unparsed, as is)
Offline
#10 2006-08-10 13:44:54
- Mary
- Sock Enthusiast
- Registered: 2004-06-27
- Posts: 6,236
Re: Parsing URLs in Comments
Parse error: syntax error, unexpected ‘>’
I repeat: make sure you’re using the plugin template correctly. I’ve already explained how.
Fatal error: Call to undefined function ace_easylinks_convert()
You didn’t use the code exactly as I posted it to you.
Edit: Dumb question.. I notice that most plugins and taghandlers have extract($atts) along with some global variables… could that be the reason this doesnt work?
The only problem is that now I get no output.
No. The reason it’s not working is because of the two points I mentioned above.
Offline
#11 2006-08-10 18:00:18
- Ace of Dubs
- Member

- Registered: 2006-04-17
- Posts: 446
Re: Parsing URLs in Comments
Mary, thanks for your guidance (and patience)
I decided to start from scratch using Inspired’s metaplugin to create this. It eliminates a lot of hassle and helps me avoid simple mistakes.
Using the script you gave me word for word, I get no errors and the page parses fine… except my body and excerpt are still not showing up, which makes me wonder about Skubidu’s post.
Hmm….
Offline
#12 2006-08-10 20:37:45
- Ace of Dubs
- Member

- Registered: 2006-04-17
- Posts: 446
Re: Parsing URLs in Comments
Looking at the source reveals that I have a stray <code></txp:ace_easylinks ></code> where my body/excerpt should be.
Offline