Hi,
I have built a little forum and am using a regular expression function to add hyperlinks when the correct BBcode is used. However, I realize that if a link is WAY TOO LONG, it breaks down the layout of the page.
Can someone help me rewrite my function to chop the string displayed (kind of like they do here on these forums) if the link is longer than 100 characters?
Here is what I have so far:
<?php
<?php
// function to add email links
function makeLinks($string) {
// add hyperlinks
$string = preg_replace("/\[url\](.*?)\[\/url\]/", '<a href="$1" target="_blank">$1</a>', $string);
// add email addresses
$string = preg_replace("/\[email\](.*?)\[\/email\]/", '<a href="mailto:$1">$1</a>', $string);
return $string;
}
?>
I would like to do something like:
<?php
if(strlen($str) > 100)
{
echo substr($str, 0, 90) . '...' . substr($str, -5, strlen($str));
}
else echo $str;
?>
... but I am not sure how to add it to my regular expression call to preg_replace()
Thanks.