Page 1 of 1

how can i do this?

Posted: Sun Feb 19, 2006 10:30 pm
by dotsc
Hi,

I'm a newbie so this might sound easy to some. This is what I want to do do.

I pull up a certain text from the database and for example:
If the text has a url in it http://www.yahoo.com or http://www.yahoo.com I would like to have code which would automatically make a link.
I don't want to put the <a href=.. tags in the text.

Any help would be appriciated.

Thanks!

Posted: Sun Feb 19, 2006 10:42 pm
by feyd
download the source code to phpbb. in the includes/bbcode.php file there is a function called make_clickable() that can perform this action.

Posted: Sun Feb 19, 2006 10:56 pm
by nickman013
Or the easier way would be this

1. Make the mysql query into a string.

2. echo this

Code: Select all

echo '<a href=$query>$query</a>';

Posted: Sun Feb 19, 2006 11:00 pm
by feyd
That doesn't make sense nickman. A MySQL query will not be a URL. The URLs are contained in the text returned from MySQL.

Posted: Mon Feb 20, 2006 12:35 am
by nickman013
Well I meant the results from the query, which contains the url.

EDIT: also it doesnt matter because i just noticed he doesnt want anchor tags in the page.

Posted: Mon Feb 20, 2006 11:02 am
by John Cartwright
what anchor tags?

Posted: Mon Feb 20, 2006 12:05 pm
by nickman013
<a href=$query>$query</a>

<a <-- doesnt 'a' stand for anchor?

Posted: Mon Feb 20, 2006 12:08 pm
by feyd
yes, <a> is called an anchor.

The OP, from what I can see doesn't want the HTML required to create the link in the stored text. That doesn't mean the anchors can't be in the output; that's just silly. :lol:

Posted: Wed Feb 22, 2006 3:51 pm
by dotsc
Thanks I will try using the bbcode option :-)

Will let you know how it goes

DotSC

Posted: Tue Feb 28, 2006 12:17 am
by dotsc
Hi this is my new code i came up with from the phpbb code:

Code: Select all

<?

echo nl2br(preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $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]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ad['addesc']);

	// 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)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ad['addesc']);

	// 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>", $ad['addesc']);

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

	return($ad['addesc']);

?>
My original code in the script was:

Code: Select all

<?php
echo nl2br(preg_replace("/\[URL\](.*)\[\/URL\]/iU", "<a href=\"\\1\" target=\"_blank\">\\1</a>$link_append", $ad['addesc']));
?>
I've tried using the new code but for some reason its not working and my text doesn't want to show up. Can anyone tell me what im doing wrong?

Thanks in advance,
DotSC