Page 1 of 1
Changing URL's
Posted: Sun Feb 23, 2003 11:17 pm
by Mr. Tech
Hi!
Do you know with this forum when your put:
[url]
http://www.domain.com[/url]
or:
[url=
http://www.domain.com]Click Here[/url]
it puts:
<a href="
http://www.domain.com" target="_blank">http://www.domain.com</a>
How can I do that?
Thanks
Tech
Posted: Sun Feb 23, 2003 11:25 pm
by redJag
How can you do what? Write code to change the syntax?
Posted: Mon Feb 24, 2003 12:27 am
by Mr. Tech
I wonder... Could it be something like this:
Code: Select all
<?php
$string = str_replace("[url=$url]$title[/url]","<a href="$url" target="_blank">$title</a>",$string);
return $string;
?>

Posted: Mon Feb 24, 2003 3:01 am
by volka
there is plenty of example code and it isn't that easy
e.g.
http://www.phpclasses.org/mirrors.html? ... 2F818.html
Posted: Mon Feb 24, 2003 3:45 am
by Mr. Tech
OK, thanks! I'll have a look
Posted: Mon Feb 24, 2003 7:01 am
by superwormy
$newlink = str_replace ("
", "<a href=\"", $oldlink);
$newlink = str_replace ("", "\">mylinkname</a>", $newlink);
Posted: Mon Feb 24, 2003 5:02 pm
by Mr. Tech
Thanks wormy but this seems to work:
For [url=
http://www.url.com]Link Text[/url]
$text = eregi_replace("\\[url=([^\\[]*)\\]([^\\[]*)\\[/url\\]","<a href=\"\\1\" target=\"_blank\">\\2</a>",$text);
For [url]
http://www.url.com[/url]
$text = eregi_replace("\\[url]([^\\[]*)\\[/url\\]","<a href=\"\\1\" target=\"_blank\">\\1</a>",$text);
Works well!
Also, with phpBB if you don't put a [url] and [/url] around your links it still makes it a link... how can do this?
Maybe:
$text = eregi_replace("\\http://([^\\[]*)\\","<a href=\"http://\\1\" target=\"_blank\">\\1</a>",$text);
Posted: Mon Feb 24, 2003 11:04 pm
by Mr. Tech
I've got this:
$article = preg_replace("#([\n ])([a-z]+?)://([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)#i", "\\1<a href=\"\\2://\\3\" target=\"_blank\">\\2://\\3</a>", $article);
But it doesn't work... Any suggestions?
Posted: Tue Feb 25, 2003 12:22 am
by hob_goblin
Heres how I do it (many thanks to phpBB for the approach):
I have a template file, containing something like:
Code: Select all
<!-- BEGIN url --><a href="{URL}" target="_blank">{DESCRIPTION}</a><!-- END url -->
name it like, url.tpl
then have a function:
Code: Select all
function opentemplate($template){
$tpl = fread(fopen($template, 'r'), filesize($template));
$tpl = str_replace('\'', '\\\'', $tpl);
$tpl = str_replace('''', '\\''', $tpl);
$tpl = str_replace("\n", '', $tpl);
$tpl = preg_replace('#<!-- BEGIN (.*?) -->(.*?)<!-- END (.*?) -->#', "\n" . '$code_tplsї''\\1''] = ''\\2'';', $tpl);
$code_tpls = array();
eval($tpl);
return $code_tpls;
}
then have another function:
Code: Select all
function preparetemplate($code_tpl){
// We do URLs in several different ways..
$code_tplї'url1'] = str_replace('{URL}', '\\1\\2', $code_tplї'url']);
$code_tplї'url1'] = str_replace('{DESCRIPTION}', '\\1\\2', $code_tplї'url1']);
$code_tplї'url2'] = str_replace('{URL}', 'http://\\1', $code_tplї'url']);
$code_tplї'url2'] = str_replace('{DESCRIPTION}', '\\1', $code_tplї'url2']);
$code_tplї'url3'] = str_replace('{URL}', '\\1\\2', $code_tplї'url']);
$code_tplї'url3'] = str_replace('{DESCRIPTION}', '\\3', $code_tplї'url3']);
$code_tplї'url4'] = str_replace('{URL}', 'http://\\1', $code_tplї'url']);
$code_tplї'url4'] = str_replace('{DESCRIPTION}', '\\2', $code_tplї'url4']);
return $code_tpl;
}
and one more function
Code: Select all
function codeparse($text){
$text = " " . $text;
global $code_tpl;
$patterns = array();
$replacements = array();
$patternsї1] = "#\їurl\](їa-z]+?://){1}(їa-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)\ї/url\]#si";
$replacementsї1] = $code_tplї'url1'];
$patternsї2] = "#\їurl\](їa-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)\ї/url\]#si";
$replacementsї2] = $code_tplї'url2'];
$patternsї3] = "#\їurl=(їa-z]+?://){1}(їa-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)\](.*?)\ї/url\]#si";
$replacementsї3] = $code_tplї'url3'];
$patternsї4] = "#\їurl=(їa-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)\](.*?)\ї/url\]#si";
$replacementsї4] = $code_tplї'url4'];
$text = preg_replace($patterns, $replacements, $text);
return trim($text);
}
then:
Code: Select all
$code_tpl = opentemplate('url.tpl');
$code_tpl = preparetemplate($code_tpl);
echo codeparse($text);
And that should parse it all! Sorry if its a bit big, but it works.
Posted: Tue Feb 25, 2003 1:12 am
by Mr. Tech
The problem is, I don't use that template system..... But i did get this from phpBB:
Code: Select all
<?php
$article = preg_replace("#([\n ])([a-z]+?)://([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)#i", "\\1<a href="\\2://\\3" target="_blank">\\2://\\3</a>", $article);
?>
Does anyone know why it isn't working?