dougan1330 2013-07-23 06:32
浏览 216
已采纳

如何使用工具提示限制要显示的名称长度和显示全名

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 "..."?

  • 写回答

5条回答 默认 最新

  • duanpang2751 2013-07-23 07:20
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?