Jump to content

Recommended Posts

Posted (edited)

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

Share this post


Link to post
Share on other sites

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

 

Which one do you think you have there? Can you find the one you want?

Share this post


Link to post
Share on other sites

Besides the advice that Requinex has provided, the substr function counts 'characters', not 'words'. If you really meant to say that you wanted only 15 words, then you need to do something else.

Share this post


Link to post
Share on other sites

 

I've tried this, but nothing is happening. 

 

 

What is nothing here?

 

You don't see any output? You see incorrect output? You get any error? Page is empty and has nothing on it?

Share this post


Link to post
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.

Share this post


Link to post
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) . '…';

Share this post


Link to post
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) . '…';
?>

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

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

How dare you sir, besmirch the reputation of the mighty Wordpress!

Share this post


Link to post
Share on other sites
Posted (edited)

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.