toolman Posted May 9, 2018 Share Posted May 9, 2018 (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 May 9, 2018 by requinix fixing bbcode Quote Link to comment Share on other sites More sharing options...
requinix Posted May 9, 2018 Share Posted May 9, 2018 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? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 10, 2018 Share Posted May 10, 2018 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. Quote Link to comment Share on other sites More sharing options...
phpmillion Posted May 10, 2018 Share Posted May 10, 2018 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? Quote Link to comment Share on other sites More sharing options...
toolman Posted May 10, 2018 Author Share Posted May 10, 2018 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 10, 2018 Share Posted May 10, 2018 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) . '…'; Quote Link to comment Share on other sites More sharing options...
toolman Posted May 10, 2018 Author Share Posted May 10, 2018 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) . '…'; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted May 10, 2018 Share Posted May 10, 2018 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? Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 10, 2018 Share Posted May 10, 2018 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! Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 10, 2018 Share Posted May 10, 2018 (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 May 10, 2018 by Psycho Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 10, 2018 Share Posted May 10, 2018 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 Quote Link to comment Share on other sites More sharing options...
maxmito Posted April 11, 2020 Share Posted April 11, 2020 (edited) I had same problem and used wpjm_get_the_job_description instead, it allows then to limit chars or word with substr or any other functions Edited April 11, 2020 by maxmito Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.