Truncate & tag long URLs?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philipp Lenssen

    Truncate & tag long URLs?

    I'm looking for a way to truncate/ add-spaces & tag long URLs, so that

    "Check this out http://www.example.com/hello-world/foo-bar.html it's
    great"

    would turn into

    "Check this out <a
    href="http://www.example.com/hello-world/foo-bar.html">http://www.exampl
    e.com/hello-world/fo...</a> it's great"

    or

    "Check this out <a
    href="http://www.example.com/hello-world/foo-bar.html">http://
    www.example.com/ hello-world/ foo-bar.html</a> it's great"

    This is because my discussion forums width is restricted and long URLs
    break this.

    Thanks!


    -------------- Current attempts...

    Adds a class which I then set to display blog, fixed width, and
    overflow auto (creates newline problem though):

    function tagLinks($str)
    {
    $pattern =
    '#(^|[^\"=]{1})(http://|https://|ftp://|mailto:|news:) ([^\s<>]+)([\s\n<>
    ]|$)#sm';
    $str = preg_replace($p attern, "\\1<a class=\"forumli nk\"
    href=\"\\2\\3\" >\\2\\3</a>\\4", $str);
    $str = str_replace("\" >http://", "\">", $str);
    return $str;
    }

    Cuts off after domain (which is really bad at times):

    function tagLinks($str)
    {
    $str = str_replace("-http:", "-temp:", $str);
    $str = str_replace("<b r />", " <br />", $str);
    $str = preg_replace(
    '/(http|https|ftp )+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)?/i', '<em><a
    href="\\0">\\4</a></em>', $str );
    $str = str_replace(" <br />", "<br />", $str);
    $str = str_replace("-temp:", "-http:", $str);
    return $str;
    }


    You can see the forum at <http://blog.outer-court.com/forum>.
  • Pedro Graca

    #2
    Re: Truncate &amp; tag long URLs?

    Philipp Lenssen wrote:[color=blue]
    > I'm looking for a way to truncate/ add-spaces & tag long URLs, so that
    >
    > "Check this out http://www.example.com/hello-world/foo-bar.html it's
    > great"
    >
    > would turn into
    >
    > "Check this out <a
    > href="http://www.example.com/hello-world/foo-bar.html">http://www.exampl
    > e.com/hello-world/fo...</a> it's great"[/color]

    Check out this thread:



    --
    USENET would be a better place if everybody read: | to email me: use |
    http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
    http://www.netmeister.org/news/learn2quote2.html | header, textonly |
    http://www.expita.com/nomime.html | no attachments. |

    Comment

    • Philipp Lenssen

      #3
      Re: Truncate &amp; tag long URLs?

      Pedro Graca wrote:
      [color=blue]
      > Philipp Lenssen wrote:[color=green]
      > > I'm looking for a way to truncate/ add-spaces & tag long URLs[/color][/color]
      [color=blue]
      >
      > http://groups.google.com/groups?thre...245.39877e38%4
      > 0posting.google .com[/color]

      Thanks, that works great!

      --
      Google Blogoscoped
      A daily news blog and community covering Google, search, and technology.

      Comment

      Working...