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 "..."?
You are essentially asking two different questions here.
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.
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.
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
echo substr('arnold schwarzenegger', 0, 6)."..."; // arnold...
Syntax:
echo substr($variable, START, END)."WRITE ANYTHING END OF LINE";
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
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