Welcome to Geeklog, Anonymous Thursday, March 28 2024 @ 12:22 pm EDT

Geeklog Forums

A few things involving text replacement


RobertM

Anonymous
Hello, I recently installed GL 1.3.9 and love it so far. I am new to php, and was looking to do a few things (mainly because i've become used to them in phpbb2 forums lol).

I was going to just try and duplicate the censored word portion, but I can't seem to find the script for this, only the data arrays (i guess thats the term)

But I was wanting urls to automatically become a hyperlink if I didn't use the 'a' tag. Also I was wanting to use a smilie system like phpbb where a normal text smile is replaced by an image.

If someone could point me to the location of the original censoring script portion, I can probably figure it out myself, thanks a bunch.
 Quote

Status: offline

JohnnyWormtown

Forum User
Junior
Registered: 05/30/04
Posts: 23
there are some good scripts already written for this function and I could've sworn I read about one of them on this forum... it must've been another geeklog related forum.

I'm looking to do the same thing for URLs and possible eMail addresses as well.

There was some talk about running the check before the submission was saved to the database and replacing the text then... is that what the censor feature does?
 Quote

Status: offline

JohnnyWormtown

Forum User
Junior
Registered: 05/30/04
Posts: 23
I found a function (actually, a couple) that looks relatively simple in my old phpBB code:

Text Formatted Code

function make_clickable($text)
{

        // pad it with a space so we can match things at the start of the 1st line.
        $ret = ' ' . $text;

        // matches an "xxxx://yyyy" URL at the start of a line, or after a space.
        // xxxx can only be alpha characters.
        // yyyy is anything up to the first space, newline, comma, double quote or <    $ret = preg_replace("#(^|[\n ])([\w]+?://[^ \"\n\r\t<]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);

        // matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing
        // Must contain at least 2 dots. xxxx contains either alphanum, or "-"
        // zzzz is optional.. will contain everything up to the first space, newline,
        // comma, double quote or <.
        $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);

        // matches an email@domain type address at the start of a line, or after a space.
        // Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
        $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);

        // Remove our padding..
        $ret = substr($ret, 1);

        return($ret);
}

/**
 * Nathan Codding - Feb 6, 2001
 * Reverses the effects of make_clickable(), for use in editpost.
 * - Does not distinguish between "www.xxxx.yyyy" and "http://aaaa.bbbb" type URLs.
 *
 */
function undo_make_clickable($text)
{
        $text = preg_replace("#<!-- BBCode auto-link start --><a href=\"(.*?)\" target=\"_blank\">.*?</a><!-- BBCode auto-link end -->#i", "\\1", $text);
        $text = preg_replace("#<!-- BBcode auto-mailto start --><a href=\"mailto:(.*?)\">.*?</a><!-- BBCode auto-mailto end -->#i", "\\1", $text);

        return $text;

}


 


I was thinking this function could be called fairly easily during the submission process, right before saving to the database (possibly for the forum as well)... can anyone think of any reason not to do this? Obviously, for Plain Old Text stories, I'd have to also put the second function in the appropiate place, as well (without the BBCode stuff, just to return it to some basic non-html state).

Any tips on where those appropriate places would be, or should I just hunt around again?
 Quote

Status: offline

JohnnyWormtown

Forum User
Junior
Registered: 05/30/04
Posts: 23
So I edited this a little bit and I'm now using it on my forum and my news articles...

The functions I'm using now look like this:

Text Formatted Code

/**
* Checks for urls and eMail addresses and replaces them with links
* (Stolen from phpBB and tweaked)
*/
function make_clickable($text)
{

        // pad it with a space so we can match things at the start of the 1st line.
        $ret = ' ' . $text;

        // matches an "xxxx://yyyy" URL at the start of a line, or after a space.
        // xxxx can only be alpha characters.
        // yyyy is anything up to the first space, newline, comma, double quote or <
        $ret = preg_replace("#(^|[n ])([w]+?://[^ "nrt<]*)#is", "1<a href="2" target="http">2</a>", $ret);

        // matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing
        // Must contain at least 2 dots. xxxx contains either alphanum, or "-"
        // zzzz is optional.. will contain everything up to the first space, newline,
        // comma, double quote or <.
        $ret = preg_replace("#(^|[n ])((www|ftp).[^ "tnr<]*)#is", "1<a href="http://2" target="nohttp">2</a>", $ret);

        // matches an email@domain type address at the start of a line, or after a space.
        // Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
        $ret = preg_replace("#(^|[n ])([a-z0-9&-_.]+?)@([w-]+.([w-.]+.)*[w]+)#i", "1<a href="mailto:2@3">2@3</a>", $ret);

        // Remove our padding..
        $ret = substr($ret, 1);

        return($ret);
}

/**
 * tweaked code originally by Nathan Codding - Feb 6, 2001
 * Reverses the effects of make_clickable(), for use in editpost.
 * - sort of distinguishes between "www.xxxx.yyyy" and "http://aaaa.bbbb" type URLs by the target set in make_clickable.
 *
 */
function undo_make_clickable($text)
{
        $text = preg_replace("#<a href="http://(.*?)" target="nohttp">.*?</a>#i", "1", $text);
        $text = preg_replace("#<a href="(.*?)" target="http">.*?</a>#i", "1", $text);
        $text = preg_replace("#<a href="mailto:(.*?)">.*?</a>#i", "1", $text);

        return $text;

}

 


I changed the target attributes and I'm now using them to return exactly what the author of the story typed in instead of adding or subtracting the http:// part by default

I'm not using undo_make_clickable for editing forum posts at all, but I am when editing plain old text stories.

I have two or three little bugs I haven't figured out yet.

bug #1:

conversion doesn't show up when previewing stories.
Tried to put the call to the function in the right place, but must've missed or something.

bug #2 (and three):

if the URL is the first character of a new line in an HTML formatted story, it won't be converted (works fine in plain text). The same bug appears when previewing forum posts, but when submitting, it works just fine. I haven't found the similarity between the two situations. The exception to this bug is if it's the first character in the whole story or post...

 Quote

Status: offline

Dirk

Site Admin
Admin
Registered: 01/12/02
Posts: 13073
Location:Stuttgart, Germany
Btw, have you tried the Geeklog version from CVS? It comes with a COM_makeClickableLinks function built in ...

bye, Dirk
 Quote

All times are EDT. The time is now 12:22 pm.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content