How can I use PHP to shorten a string after 25 words?

Icon ask Sir Apfelot

Icon ask Sir ApfelotI'm looking for a way to shorten a text string after a certain number of words. There is a command in PHP that allows you to shorten a string to X characters, but that's mostly in the middle of the word and I don't like that. How can you solve this with a number of words?

Answer: To do this you have to build your own function. PHP itself does not have a function that allows such word-based truncation. Basically you cut up the text with explode and use the space as a separator. Then mine has an array that contains the individual words. In order to limit the string to 25 words, you build up the text little by little with a loop and just let it run 25 times. The finished PHP script looks like this:

$ meintext = "This is my great text, which I would like to shorten. Currently it is still relatively long and contains a lot of words. But after the PHP script is finished with it, it will be quite short. About as long as one would like. Just give it a try! "; $ short text = ''; $ number words = 25; $ snippet = explode ('', $ mytext); for ($ x = 0; $ x <$ number words; $ x ++) {$ short text. = "{$ snippet [$ x]}"; } $ short text. = "..."; // add dots echo $ short text; // Finished...

 

Why words and not signs?

Separating a text after a certain number of words is much more elegant than shortening it after any number of characters. This makes the end of the excerpt much more legible and looks better. Words cut off in the middle always seem like mistakes in the text, I think.

PHP example: Shorten the string to X characters

However, if you want to shorten words instead of words after a certain number of characters for whatever reason, this is much easier than the example above. Here you only need three lines of code:

$ meintext = "This is my great text, which I would like to shorten. Currently it is still relatively long and contains a lot of words. But after the PHP script is finished with it, it will be quite short. About as long as one would like. Just give it a try! "; $ mytext = strip_tags ($ mytext); // First filter out HTML tags that would otherwise be broken when shortened if (strlen ($ mytext)> 100) echo substr ($ mytext, 0,100). "..."; // after 100 characters it is shortened else echo $ mytext;  
-
Do you like my blog? Then I would be happy to receive a short review on Google. Easy leave something here for a moment - that would be great, thank you!

 

Effectively for free: iPhone 13 Mini and iPhone 13 deals with top conditions at Otelo - Advertisement

2 comments

  1. Siegbert says:

    The representation of the code block is a bit idiosyncratic and disappears somewhere in nirvana on the right side of the browser.

    • Sir Apfelot says:

      Hello Siegbert! Thanks for the hint. That kind of broke over the years because I used to have another plugin to display the codes. But I have now corrected it and hope it is readable again!

Leave a Comment

Your e-mail address will not be published. Required fields are marked with * .