1

I have some html links, like

<a href="urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser">urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser</a>

and I need to cut the text (not the link) if it's longer more than some characters (let's say 30). So the link must become somethings like :

<a href="urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser">urlThatIne... ...TheBrowser</a>

How can I do it? Regex? I'm using PHP and jQuery (but I'd like to do it server side).

fedorqui
  • 275,237
  • 103
  • 548
  • 598
markzzz
  • 47,390
  • 120
  • 299
  • 507

4 Answers4

7

There probably is a regex method, but I'd opt for a simple substr().

if (strlen($title)>30) {
    $title=substr($title, 0, 10) . "..." . substr($title, -10);
}
Brad
  • 159,648
  • 54
  • 349
  • 530
  • You're right! In this simple case, there's no need for regular expressions. – ComFreek Nov 03 '11 at 20:58
  • No! doing this it count also the `a`, the `href` link, and the text....not only the text... – markzzz Nov 03 '11 at 21:07
  • @markzzz, You said you wanted to cut the text... I had assumed you already parsed the DOM. If you haven't done that yet, you need a DOM parser to get the inner text of that tag. yasar11732 suggested one. – Brad Nov 03 '11 at 21:08
  • DOM parser on server side? :O – markzzz Nov 03 '11 at 21:09
  • @markzzz, Yes... how did you expect to parse HTML otherwise? It's the only reliable way to do it. Examples here: http://stackoverflow.com/questions/960841/how-to-use-dom-php-parser – Brad Nov 03 '11 at 21:10
  • Didn't know it yet :) thanks you! I'll try to parse all my links and apply your strategy, that seems to be the better solution... – markzzz Nov 03 '11 at 21:14
2

Just use css.

<a href="urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser">urlThatIne... ...TheBrowser</a>

a {  
    white-space: nowrap;  
    overflow: hidden;  
    text-overflow: ellipsis;  
    width: 300px;
    -o-text-overflow: ellipsis;  
    -ms-text-overflow: ellipsis; 
} 
mccow002
  • 6,754
  • 3
  • 26
  • 36
  • [also needs to be `display: block`](http://stackoverflow.com/questions/12820007/text-overflowellipsis-on-a-link/12820195#12820195) – laggingreflex Oct 28 '14 at 10:15
1

As far as I understand, you are not creating this page but parsing it. So first, I would suggest using a dom parser (I use: http://simplehtmldom.sourceforge.net/), get the anchor element's inner text. Than use substr ( http://tr.php.net/manual/en/function.substr.php ) method to cut it.

yasar
  • 13,158
  • 28
  • 95
  • 160
1

If you want the link text to indicate that it has been truncated...

function printLink( $url ){
    $text = strlen( $url ) > 30 
          ? substr( $url, 30 ) . '&hellip;'
          : $url;

    echo '<a href="' . $url . '">' . $text . '</a>';
}

printLink( 'urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser' );
// <a href="urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser">urlThatIneedToCutBecauseItIsT…</a>
Farray
  • 8,290
  • 3
  • 33
  • 37