how can i do this?
Moderator: General Moderators
how can i do this?
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!
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!
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
Or the easier way would be this
1. Make the mysql query into a string.
2. echo this
1. Make the mysql query into a string.
2. echo this
Code: Select all
echo '<a href=$query>$query</a>';- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
Hi this is my new code i came up with from the phpbb code:
My original code in the script was:
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
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']);
?>Code: Select all
<?php
echo nl2br(preg_replace("/\[URL\](.*)\[\/URL\]/iU", "<a href=\"\\1\" target=\"_blank\">\\1</a>$link_append", $ad['addesc']));
?>Thanks in advance,
DotSC