Urll BB Code

Started on the old forum in December 2013 · view the original in the archive · old links redirect here

7 replies · 369 views · started 12 years ago · 1 min read

2 participants
tzapone Junior · 30 posts #1
function links_preg2($arr)
{
	global $set;
	if (preg_match('#^http://' . preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[2]))
	return $arr[1] . '<a href="' . $arr[2] . '">' . $arr[2] . '</a>' . $arr[4];
	else
	return $arr[1] . '<a' . ($set['web'] ? ' target="_blank"' : null) . ' href="http://' . $_SERVER['HTTP_HOST'] . '/go.php?go=' . base64_encode(html_entity_decode($arr[2])) . '">Link</a>' . $arr[4];
}

function links($msg)
{
	global $set;
	if ($set['bb_img'])$msg = preg_replace_callback('/\[img\](.+)\[\/img\]/isU', 'img_preg', $msg);
	if ($set['bb_url'])$msg = preg_replace_callback('/\[url=(.+)\](.+)\[\/url\]/isU', 'links_preg1', $msg);
	
	if ($set['bb_http'])$msg = preg_replace_callback('~(^|\s)([a-z]+://([^ \r\n\t`\'"]+))(\s|$)~iu', 'links_preg2', $msg);
	
	return $msg;
}

This is the code that i have on my site for url link's. I want to make a new think: -each time when a user post a url: http://youtube.com/watch?blablabla , i want to show on the site : Link youtube -each time when a user post a url" http://www.facebook.com/picture/etcetcetctjkjk , i want to show on the site: Link facebook Anyone have an ideea how to make something like this? I gues that with preg_replace or url_parsing but i don't know how 🙁

something else Fellow · 2,296 posts #2
$str = preg_replace('/http:\/\/(.*)./is', $1, $arr[2]);
// $str is now the site name instead of $arr[2]

//or

$str = explode(".",explode("://",$arr[2]));
// $str[1] is now the site name instead of $arr[2]

//or there is many other ways to script it :P
tzapone Junior · 30 posts #3

something else wrote:

$str = preg_replace('/http:\/\/(.*)./is', $1, $arr[2]);
// $str is now the site name instead of $arr[2]


//or

$str = explode(".",explode("://",$arr[2]));
// $str[1] is now the site name instead of $arr[2]

//or there is many other ways to script it :P

where i put this?

function links_preg2($arr)
{
	global $set;
	if (preg_match('#^http://' . preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[2]))
	return $arr[1] . '<a href="' . $arr[2] . '">' . $arr[2] . '</a>' . $arr[4];
	else
	return $arr[1] . '<a' . ($set['web'] ? ' target="_blank"' : null) . ' href="http://' . $_SERVER['HTTP_HOST'] . '/go.php?go=' . base64_encode(html_entity_decode($arr[2])) . '">Link</a>' . $arr[4];
}

i can't do it, Warning: explode() expects parameter 2 to be string, array given in

something else Fellow · 2,296 posts #4

tzapone wrote: where i put this?

i can't do it, Warning: explode() expects parameter 2 to be string, array given in

oops i missed an array key out in the 2nd code >.<

try this:

function links_preg2($arr)
{
	global $set;
        $str = preg_replace('/http:\/\/(.*)./is', $1, $arr[2]); 
	if (preg_match('#^http://' . preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[2]))
	return $arr[1] . '<a href="' . $arr[2] . '">' . $str . '</a>' . $arr[4];
	else
	return $arr[1] . '<a' . ($set['web'] ? ' target="_blank"' : null) . ' href="http://' . $_SERVER['HTTP_HOST'] . '/go.php?go=' . base64_encode(html_entity_decode($arr[2])) . '">Link</a>' . $arr[4];
}
tzapone Junior · 30 posts #5

something else wrote: oops i missed an array key out in the 2nd code >.<

try this:

function links_preg2($arr)
{
	global $set;
        $str = preg_replace('/http:\/\/(.*)./is', $1, $arr[2]); 
	if (preg_match('#^http://' . preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[2]))
	return $arr[1] . '<a href="' . $arr[2] . '">' . $str . '</a>' . $arr[4];
	else
	return $arr[1] . '<a' . ($set['web'] ? ' target="_blank"' : null) . ' href="http://' . $_SERVER['HTTP_HOST'] . '/go.php?go=' . base64_encode(html_entity_decode($arr[2])) . '">Link</a>' . $arr[4];
}

Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in on line

$str = preg_replace('/http:\/\/(.*)./is', $1, $arr[2]);  

Added after 3 minutes:

fixed: i put $arr[1] instead $1 .

Now i put

 '#^(https://|http://)?(www|m)?\.?(youtube\.com|youtu.be)(/watch(\.php)?\?v=|/v/|/)(?P<id>[a-zA-Z0-9-_]{11})(?P<rest>[\w&=/\?\.\#\+]+)?#ism'. '#'

in $str?

Added after 17 minutes:

i put this code:

function links_preg2($arr) 
{ 
    global $set; 
        $str = preg_replace('#(https://|http://)?(www|m)?\.?(youtube\.com|youtu.be)(/watch(\.php)?\?v=|/v/|/)(?P<id>[a-zA-Z0-9-_]{11})(?P<rest>[\w&=/\?\.\#\+]+)?#ism', $arr[1], $arr[2]);
		
    if (preg_match('#^http://' . preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[2])) 
    return $arr[1] . '<a href="' . $arr[2] . '">' . $str . '</a>' . $arr[4]; 
    else 
    return $arr[1] . '<a' . ($set['web'] ? ' target="_blank"' : null) . ' href="http://' . $_SERVER['HTTP_HOST'] . '/go.php?go=' . base64_encode(html_entity_decode($arr[2])) . '">Link</a>' . $arr[4]; 
} 
function links($msg)
{
	global $set;
	if ($set['bb_img'])$msg = preg_replace_callback('/\[img\](.+)\[\/img\]/isU', 'img_preg', $msg);
	if ($set['bb_url'])$msg = preg_replace_callback('/\[url=(.+)\](.+)\[\/url\]/isU', 'links_preg1', $msg);
	
	if ($set['bb_http'])$msg = preg_replace_callback('~(^|\s)([a-z]+://([^ \r\n\t`\'"]+))(\s|$)~iu', 'links_preg2', $msg);

but don't work: when i put this link on status http://www.youtube.com/watch?v=YAJ9ArSlUG0 doesn't appear Link Youtube, only Link

something else Fellow · 2,296 posts #6

ok i tried it myself and i forgot the quotes around the "$1"

So i tested this:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$arr[2] = "http://google.com";
$str = preg_replace('/http:\/\/(.*)./is', "$1", $arr[2]); 
echo $str;
?>

However this gave an unexpected result: google.co to which i am slighhtly confused to why it is doing that >.< arnage is good at preg_replace so he might be able to tell whats wrong with it.

anyway here is a working version:

function links_preg2($arr) 
{ 
    global $set; 
     $str = explode("://",$arr[2]);
    $str = explode(".",$str[1]); 
    if (preg_match('#^http://' . preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[2])) 
    return $arr[1] . '<a href="' . $arr[2] . '">' . $str[0] . '</a>' . $arr[4]; 
    else 
    return $arr[1] . '<a' . ($set['web'] ? ' target="_blank"' : null) . ' href="http://' . $_SERVER['HTTP_HOST'] . '/go.php?go=' . base64_encode(html_entity_decode($arr[2])) . '">Link</a>' . $arr[4]; 
} 
tzapone Junior · 30 posts #7
function links_preg2($arr)  
{  
    global $set;  
     $str = explode("://",$arr[2]); 
    $str = explode("/",$str[1]);  
    if (preg_match('#^http://' . preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[2]))  
    return $arr[1] . '<a href="' . $arr[2] . '">' . $str[0] . '</a>' . $arr[4];  
    else  
    return $arr[1] . '<a' . ($set['web'] ? ' target="_blank"' : null) . ' href="http://' . $_SERVER['HTTP_HOST'] . '/go.php?go=' . base64_encode(html_entity_decode($arr[2])) . '">Link '.$str[0].'</a>' . $arr[4];  
}

I did it with this function 😀

Thank's for the help.

The topic can be closed.

something else Fellow · 2,296 posts #8

Your welcome 🙂

btw error was with (.*) regex in the preg_replace it needed [^a-zA-Z0-9] regex the end "." must be to go back 1 letter which produce the .co instead of .com

Anyway, using explode or preg_replace fails due to sub-domains eg: http://m.youtube.com which will produces the letter "m"

but that's as far as I'm willing to script atm 😛

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close