1

if I have to echo a long name out

echo "arnold schwarzenegger";

I want it to limit the word length and display the first 6 char, and display the rest in "..."

arnold...

How to do it? and is there a way for to display the long name when I hover my cursor over the "..."?

Ryan
  • 155
  • 1
  • 5
  • 19

5 Answers5

2

Here comes a custom function which can do the job:

function shorten($str, $maxlen) {
    if(strlen($str) <= $maxlen - 3) {
        return $str;
    } else {
        return substr($str, 0, $maxlen - 3) . '...';
    }
}

echo shorten('arnold schwarzenegger', 10); // output: arnold ...

If output is HTML, then beside from a PHP solution there might be a CSS solution as well. My CSS is a little bit rusty, but you may find help here: wordwrap a very long string

Community
  • 1
  • 1
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
1
echo substr('arnold schwarzenegger', 0, 6)."...";  // arnold...

Syntax:

echo substr($variable, START, END)."WRITE ANYTHING END OF LINE";
Bora
  • 10,529
  • 5
  • 43
  • 73
1

You are essentially asking two different questions here.

Shortening the name

This has been answered a couple of times already, but the most easy syntax would be:

strlen($name, 0, 10);

This only shows the first 10 characters. The "0" stands for the start.

I'm hoping you're using objects for this, as you can easily add a method to your model and use something like:

$user->getShortname();

This would make doing the second part a lot easier as well.

Showing the tooltip with the full name

Depending on your structure and architecture; you can easily have the original full name and the shortened name living together in the same object or in different variables.

Depending on your choice of tools (jQuery, any additional plugins you want to use, etc.), you can easily add a tooltip plugin. jQuery-UI has a nice one if I may suggest one.

Adding it depends on your choice of tools, it varies how difficult it would be to add a tooltip. With the jQuery-UI version it's as easy as:

<span class="tooltip" title="<?=$user->getFullname()?>"><?=$user->getShortname()?></span>

And add the following Javascript to your page:

$(".tooltip").tooltip();

That should work just fine :-). If you have any more specific questions for one of these parts; either update your original post or create a new question.

Bjorn 'Bjeaurn' S
  • 3,861
  • 2
  • 18
  • 29
0

Try this :

 $a ='arnold schwarzenegger';
 $result=substr(strip_tags(html_entity_decode($a, ENT_QUOTES, 'UTF-8')), 0, 20);
print_r($result);

Please see this links

VIVEK-MDU
  • 2,483
  • 3
  • 36
  • 63
0

If you are fetching your data from the database you can use MySQL LEFT to get the shorten value directly.

SELECT table_name.field_name, LEFT(table_name.field_name, 6) as shorten from table_name

This could be helpful if you are trying to get something like post excerpt of your blogging site.

Function Reference: http://www.w3resource.com/mysql/string-functions/mysql-left-function.php

Konsole
  • 3,447
  • 3
  • 29
  • 39