how can i do this?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dotsc
Forum Newbie
Posts: 14
Joined: Sun Jan 29, 2006 2:55 pm

how can i do this?

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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>';
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what anchor tags?
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

<a href=$query>$query</a>

<a <-- doesnt 'a' stand for anchor?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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:
dotsc
Forum Newbie
Posts: 14
Joined: Sun Jan 29, 2006 2:55 pm

Post by dotsc »

Thanks I will try using the bbcode option :-)

Will let you know how it goes

DotSC
dotsc
Forum Newbie
Posts: 14
Joined: Sun Jan 29, 2006 2:55 pm

Post 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
Post Reply