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="&#123;URL&#125;" target="_blank">&#123;DESCRIPTION&#125;</a><!-- END url -->
name it like, url.tpl

then have a function:

Code: Select all

function opentemplate($template)&#123;
	$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&#1111;''\\1''] = ''\\2'';', $tpl);
	
	$code_tpls = array();

	eval($tpl);
	
	return $code_tpls;
&#125;
then have another function:

Code: Select all

function preparetemplate($code_tpl)&#123;
	// We do URLs in several different ways..
	$code_tpl&#1111;'url1'] = str_replace('&#123;URL&#125;', '\\1\\2', $code_tpl&#1111;'url']);
	$code_tpl&#1111;'url1'] = str_replace('&#123;DESCRIPTION&#125;', '\\1\\2', $code_tpl&#1111;'url1']);
	
	$code_tpl&#1111;'url2'] = str_replace('&#123;URL&#125;', 'http://\\1', $code_tpl&#1111;'url']);
	$code_tpl&#1111;'url2'] = str_replace('&#123;DESCRIPTION&#125;', '\\1', $code_tpl&#1111;'url2']);
	
	$code_tpl&#1111;'url3'] = str_replace('&#123;URL&#125;', '\\1\\2', $code_tpl&#1111;'url']);
	$code_tpl&#1111;'url3'] = str_replace('&#123;DESCRIPTION&#125;', '\\3', $code_tpl&#1111;'url3']);
	
	$code_tpl&#1111;'url4'] = str_replace('&#123;URL&#125;', 'http://\\1', $code_tpl&#1111;'url']);
	$code_tpl&#1111;'url4'] = str_replace('&#123;DESCRIPTION&#125;', '\\2', $code_tpl&#1111;'url4']);

	return $code_tpl;
&#125;
and one more function ;)

Code: Select all

function codeparse($text)&#123;


      $text = " " . $text; 

	global $code_tpl;

	$patterns = array();
	$replacements = array();


	$patterns&#1111;1] = "#\&#1111;url\](&#1111;a-z]+?://)&#123;1&#125;(&#1111;a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)\&#1111;/url\]#si";
	$replacements&#1111;1] = $code_tpl&#1111;'url1'];


	$patterns&#1111;2] = "#\&#1111;url\](&#1111;a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)\&#1111;/url\]#si";
	$replacements&#1111;2] = $code_tpl&#1111;'url2'];


	$patterns&#1111;3] = "#\&#1111;url=(&#1111;a-z]+?://)&#123;1&#125;(&#1111;a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)\](.*?)\&#1111;/url\]#si";
	$replacements&#1111;3] = $code_tpl&#1111;'url3'];


	$patterns&#1111;4] = "#\&#1111;url=(&#1111;a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)\](.*?)\&#1111;/url\]#si";
	$replacements&#1111;4] = $code_tpl&#1111;'url4'];


	$text = preg_replace($patterns, $replacements, $text);


	return trim($text);
&#125;
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?