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.

    i use something like this:

    if (strlen($string) >100) {
        $string_text = substr_replace ($string, "...", 100);
    }
    else {
        $string_text = $string;
    }
    
    return $string_text;
    

    and just put it after your preg_replace

      Hi, revdev:

      Thanks for your reply.

      The string in question can be a very long string, as it represents a forum post. it is OK if the entire string length is greater than 100 characters, but not the displayed text within the [ url ] bb-code link tags.

      So I could use your idea, but I just don't know how to incorporate it into my function, that I pass the looooooooooooong string....

      Does that make sense? Can you help me with this?

      Thanks.

        Hi,

        one way is to add the e modifier to your pattern so you can add functions to your replacement string.

        I modified the first preg_replace line in your code:

        function makeLinks($string) {
        
        // add hyperlinks
        $string = preg_replace("/\[url\](.*?)\[\/url\]/e", "'<a href=\"\\1\" target=\"_blank\">'.substr('\\1',0,100).'</a>'", $string);
        
        // add email addresses
        $string = preg_replace("/\[email\](.*?)\[\/email\]/", '<a href="mailto:$1">$1</a>', $string);
        return $string;
        }
        

        Thomas

          Thomas,

          Thanks for that. I can use that, but would really, really like it IF the link is longer than 100 characters, to be able to display it in the following way:

          display first 90 characters
          then dot, dot, dot (...)
          then the last five characters

          Can you help me with this, or point me to a tutorial?

          Danke schonn.

            That's possible,

            my example already contains everything that you need:

            1. string concatenation in the replacement string
            2. substr (you can use substr twice in the replacement string)

            One hint:

            substr($str,-5);

            returns the last five characters of a string.

            Thomas

              Write a Reply...