Jump to content

[SOLVED] function hyperlink () | Truncate anchortext?


Yves

Recommended Posts

Hello. Below you see a function making hyperlinks out of url's in a text string. It works perfectly. However I would like to see the anchortext cut (truncated is the right term I believe) to display 40 characters.

 

<?php
function hyperlink ($string) {
	$string = preg_replace('#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)#is', '\\1<a href="\\2" target="_blank" rel="nofollow">\\2</a>', $string);
	$string = preg_replace('#(^|\s)((www|ftp)\.([^\s\w/]?[\w/])*)#is', '\\1<a href="http://\\2" target="_blank" rel="nofollow">\\2</a>', $string);
	return $string;
	}
?>

 

Example

 

display this:

 

<a href="http://www.domain.com/folder/the-pagetitle-goes-right-here.html">http://www.domain.com/folder/title-goes-...</a>

 

isntead of this:

 

<a href="http://www.domain.com/folder/title-goes-right-here.html">http://www.domain.com/folder/title-goes-right-here.html</a>

 

http://www.domain.com/folder/title-goes- is exactly 40 characters. Notice the "..." at the end of the anchortext too. I hope somebody is kind enough to alter the above function for me. Thanks.

Your function returns a string. Your script (some other piece that you didn't post) makes it into a link.  You want to show only 40 chars of that link string, with a ... at the end of it. So you take the string that's returned from your function, display it in the href = '...', use  substr() to show the short version of it between the anchor tags.

 

I assume that this is what your code looks like (the principle, i'm not a mind reader):

$string = hyperlink($somethingtoparse);
echo "<a href = '$string'>$string</a>";

 

so change it to this:

$string = hyperlink($somethingtoparse);
echo "<a href = '$string'>". substr($string,0,40) . "...</a>";

 

Ah. I see what you mean, but...

 

The string is actually a piece of text. example: "This is an example sentence. This is my test site: http://www.mysite.com Go check it out." this is the string that's going through the function. It leaves text as is and changes http links into hyperlinks. So, I can't use your above suggestion in this case. The function will always check a large text block for urls and make those urls clickable.

 

What I want is when the function finds the urls, it has make the urls active but also truncate the longer-than-40-characters urls.

 

 

To tamper with the replacements, you can use preg_match_callback(). Have a look at this:

 

<?php
function evaluate1($matches) {
return $matches[1] . '<a href="' . $matches[2] . '" target="_blank" rel="nofollow">' . substr($matches[2], 0, 40) . '...</a>';
}
function evaluate2($matches) {
return $matches[1] . '<a href="http://' . $matches[2] . '" target="_blank" rel="nofollow">' . substr($matches[2], 0, 40) . '...</a>';
}
function hyperlink($string) {
$string = preg_replace_callback(
	'#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)#is',
	'evaluate1',
	$string
);
$string = preg_replace_callback(
	'#(^|\s)((www|ftp)\.([^\s\w/]?[\w/])*)#is',
	'evaluate2',
	$string
);
return $string;
}
$string = 'This is an example sentence. This is my test site: http://www.domain.com/folder/the-pagetitle-goes-right-here.html Go check it out.';
echo hyperlink($string);
?>

 

The matches are sent to the specified functions (evaluate1 and evaluate2) as an array of parameters, and these functions are then evaluated. I made them return the strings you're looking for, with the truncated anchor text. $matches[1] and $matches[2] are similar to the back references \\1 and \\2 you were using.

That's exactly what I wanted. I did alter it slightly, but it's nothing compared to what you came up with!

 

<?php
function evaluate1($matches) {
if (strlen($matches[2]) > 40 ) {
return $matches[1] . '<a href="' . $matches[2] . '" target="_blank" rel="nofollow">' . substr($matches[2], 0, 40) . '...</a>';
} else {
return $matches[1] . '<a href="' . $matches[2] . '" target="_blank" rel="nofollow">' . $matches[2] . '</a>';
}
}
function evaluate2($matches) {
if (strlen($matches[2]) > 40 ) {
return $matches[1] . '<a href="http://' . $matches[2] . '" target="_blank" rel="nofollow">' . substr($matches[2], 0, 40) . '...</a>';
} else {
return $matches[1] . '<a href="http://' . $matches[2] . '" target="_blank" rel="nofollow">' . $matches[2] . '</a>';
}
}
function hyperlink($string) {
$string = preg_replace_callback(
	'#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)#is',
	'evaluate1',
	$string
);
$string = preg_replace_callback(
	'#(^|\s)((www|ftp)\.([^\s\w/]?[\w/])*)#is',
	'evaluate2',
	$string
);
return $string;
}
?>

 

Thanks. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.