Jump to content

truncate text


toolman

Recommended Posts

Hi there,

 

I'm trying to truncate some text by 15 words which is then followed by a "..."

 

This is the line that outputs all the text:

 

wpjm_the_job_description(); 
 

I've tried this, but nothing is happening. 

 


<?php $desc=wpjm_the_job_description(); 
echo substr($desc, 0, 15); ?>
 

Any ideas what I have wrong?

 

 

Thanks

Edited by requinix
fixing bbcode
Link to comment
Share on other sites

Hi,

 

Thanks for the replies.

 

I'm happy with the one I have above, so if there is a way I can truncate that, that would be ideal.

 

With regards to phpmillion, it is outputting everything, but what I meant was the code I have is not doing anything, so I guess I have something wrong with it. I am happy the character count, but ideally word count would be better.

Link to comment
Share on other sites

Try

$text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci. Aenean nec lorem.";

$words = array_slice(explode(' ', $text), 0, 15);

echo join(' ', $words) . '…';

Link to comment
Share on other sites

Hi there,

 

Thank you for the reply.

 

Thanks, that works great with the text if its already set. However, If I try to pull it through using the WordPress function I have, It outputs the whole text.

 

This is what I tried:

 

<?php 
    $text = wpjm_the_job_description();
    $words = array_slice(explode(' ', $text), 0, 15);
    echo join(' ', $words) . '…';
?>
Link to comment
Share on other sites

You could try finding out what the text actually contains (seems like there are no spaces)

 

$text =  wpjm_the_job_description();

$short = substr($text,0,25);    // get first 25 characters
$chrs = str_split($short);      // split into individual chars
$hex = array_map('bin2hex', $chrs);    // get the hex values of those chars

echo '<pre>', print_r($chrs, 1), '</pre>';     // output to compare
echo '<pre>', print_r($hex, 1), '</pre>';      // output to compare

 

What is the output?

Link to comment
Share on other sites

Hi there,

 

Thank you for the reply.

 

Thanks, that works great with the text if its already set. However, If I try to pull it through using the WordPress function I have, It outputs the whole text.

 

This is what I tried:

<?php 
    $text = wpjm_the_job_description();
    $words = array_slice(explode(' ', $text), 0, 15);
    echo join(' ', $words) . '…';
?>

 

Please read requinix's earlier response.

 

Like much of the stupidness that is WordPress, there's one function to output something and another function to get the value.

 

If you are still getting the full output with your code above, that is because the function wpjm_the_job_description() is outputting the content to the page and not returning it. E.g.

wpjm_the_job_description() {
    $value = "Get some text from some process or source to be displayed";
    echo $value; //The function is directly outputting text to the page
    return; //Nothing is returned from the function for you to modify it
}

You will either need to see if there is a function to get the string rather than outputting it or you can try modifying that function directly.

Edited by Psycho
Link to comment
Share on other sites

You will either need to see if there is a function to get the string rather than outputting it or you can try modifying that function directly.

 

You'll be better off if you can find a function that returns the string, instead of outputting it as already mentioned. If one's not available, you could also consider catching the output with Output Buffering.

http://php.net/manual/en/book.outcontrol.php

Link to comment
Share on other sites

  • 1 year later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.