Hi friends

This is Radhika

Here is the Query:


$str=strlen("Welcome to Program of PHP");
this is o/p:25
But my query is
it should read how many words we like
for example:uptill "welcome":
output is :7....... remaining part should be printed like dots etc.
But use single function only.


advance thanks
Radhika

Dani AI

Generated

A compact, single-function solution that follows 's split-into-words idea and answers 's question about inputs/outputs: the function below returns the truncated display text plus both character counts (with and without the appended dots). That makes 's example explicit — the first word "Welcome" has 7 characters, the displayed string "Welcome..." has 10.

function truncate_words($text, $num_words = 1, $ellipsis = '...') {
    $text = trim($text);
    if ($text === '') {
        return ['text'=>'', 'chars_without_ellipsis'=>0, 'chars_with_ellipsis'=>0, 'was_truncated'=>false];
    }
    $words = preg_split('/\s+/', $text);
    $first = implode(' ', array_slice($words, 0, $num_words));
    $was_truncated = count($words) > $num_words;
    $truncated = $was_truncated ? $first . $ellipsis : $first;
    return [
        'text' => $truncated,
        'chars_without_ellipsis' => strlen($first),
        'chars_with_ellipsis' => strlen($truncated),
        'was_truncated' => $was_truncated
    ];
}

// example:
// $r = truncate_words('Welcome to Program of PHP', 1);
// $r['text'] => 'Welcome...'
// $r['chars_without_ellipsis'] => 7
// $r['chars_with_ellipsis'] => 10

Notes and edge cases:

  • Extra whitespace handled via preg_split; multiple spaces won't break the count.
  • For UTF-8/multibyte strings replace strlen with mb_strlen and use preg_split('/\s+/u', ...) (set mb_internal_encoding('UTF-8') as needed).
  • If input might contain HTML, run strip_tags() before truncation so tags don't count toward word/char totals.
    This single function covers extraction, display and both counts so different callers can use whichever value they need.

Recommended Answers

All 2 Replies

What exactly are you asking for? What is a word "we like"? It will probably be possible to write a function to do what you want but I'm not entirely sure what that is.

All in all, what info do you want to get out of the function and what info do you need to put in?

Radhika, the problem can be resolved very easy step by step, would be necessary first to make an explode(), separating words using a space delimiter. The result will be an array with each index followed by a word.
Now, inside the function, the for loop will iterate the passed argument (desired words quantity). On each iteration, concatenate a variable passing each array element using as key the variable used into the for loop. Note: Add a space if it's necessary too between each concatenation. Also, due to the array start with zero value, the first passed argument should be decreased once.
After the loop, you will have a variable only with some words passed, you can finally apply strlen() to count the elements.

Good luck :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.