Jump to content


Photo

truncate text


  • Please log in to reply
10 replies to this topic

#1 toolman

toolman
  • Members
  • PipPipPip
  • Advanced Member
  • 210 posts

Posted 09 May 2018 - 09:22 PM

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, 09 May 2018 - 10:34 PM.
fixing bbcode


#2 requinix

requinix
  • Administrators
  • Impoverished Administrator
  • 9,874 posts
  • LocationWA

Posted 09 May 2018 - 10:34 PM

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?
"Basically, I think the general rule of thumb is: if someone really wants the blood that's inside of your body, and they're like a vampire, or a dracula, or some sort of man-squito, then that's probably okay. A dracula and a man-squito are made for removing things like blood and swords from inside your body. That's basically fine. If something wants to get at your blood and they're, say, some kind of murdersaurus, or maybe a really big frog, that's where the problems start to arise. A really big frog is not made for removing blood, and your blood knows this, which is why it is so vehement about wanting to stay in your body instead of coming out. Unfortunately this will not deter a really big frog because a really big frog is full of things like prizes, and value, and quite a lot of hatred, and it would really rather like to replace any and all of those things with your blood, and basically by any means possible." --slumbermancer

#3 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 4,194 posts
  • LocationVoorheesville NY

Posted 10 May 2018 - 01:46 AM

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.
JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

#4 phpmillion

phpmillion
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts
  • LocationLithuania

Posted 10 May 2018 - 06:12 AM

 

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?


Do you license and update your PHP scripts?


#5 toolman

toolman
  • Members
  • PipPipPip
  • Advanced Member
  • 210 posts

Posted 10 May 2018 - 07:59 AM

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.



#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,169 posts

Posted 10 May 2018 - 09:33 AM

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) . '&hellip;';


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 toolman

toolman
  • Members
  • PipPipPip
  • Advanced Member
  • 210 posts

Posted 10 May 2018 - 10:06 AM

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) . '&hellip;';
?>


#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,169 posts

Posted 10 May 2018 - 11:09 AM

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?


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 gizmola

gizmola
  • Administrators
  • Advanced Member
  • 4,722 posts
  • LocationLos Angeles, CA USA

Posted 10 May 2018 - 03:12 PM

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!

#10 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,937 posts
  • LocationCanada

Posted 10 May 2018 - 05:15 PM

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) . '&hellip;';
?>

 

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, 10 May 2018 - 05:16 PM.

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#11 cyberRobot

cyberRobot
  • Moderators
  • Much to Learn
  • 2,793 posts

Posted 10 May 2018 - 06:36 PM

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/manua....outcontrol.php






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users