PHP! How to insert if function? Or better using another solution?

by tgma
3 replies
PHP Code:
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php echo thman_get_limited_string($post->post_title,13); ?></a>
Because there isn't enough space for the whole title the title has to be cut but this code creates a second line if the last displayed word* is too long.

Example 1
Whole title: 12345 123456 12345678* 12345678 1234567890
(2 Lines)

12345 123456
12345678*...

Example 2
hole title: 123 123456 123456 12345678 1234567890
(1 Line)

123 123456 123456...
Question:

How can I change the code to insert a <br /> if I get Example 2? And if I get Example 1 then a <br /> shouldn't be inserted.

PHP Code:
Solution
Solusion 1
Whole title: 12345 123456 12345678* 12345678 1234567890
(2 Lines)

12345 123456
12345678*...

Solution 2
hole title: 123 123456 123456 12345678 1234567890
(1 Line)

123 123456 123456...
<br />
#function #insert #php #solution
  • Profile picture of the author Tim Franklin
    This sounds like a job for my good friends, if, then else, they work really well together and hardly ever get into trouble,

    PHP If...Else Statements

    have a look at this I think it should put you on the right path to discover my good friends, say hello when you find them, they hang out just about everywhere these days,
    Signature
    Bitcoin | Crypto | Blockchain Secrets |
    {{ DiscussionBoard.errors[3177520].message }}
    • Profile picture of the author tgma
      What function I need to filter out if it reads out 2 lines or just 1 line? Does a trick exist to get that?
      {{ DiscussionBoard.errors[3177732].message }}
      • Profile picture of the author Ambius
        something like this

        PHP Code:
        if (strlen($ title)>20) {
         
        //2 lines;
        } else {
         
        //1 line;

        it's not perfect because some characters are wider than others... but it's an easy implementation.
        {{ DiscussionBoard.errors[3184128].message }}

Trending Topics