Home > other >  Display Limited word from specific String
Display Limited word from specific String

Time:11-22

I have one sentence. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor Demo post is incididunt ut labore et dolore magna aliqua.

On above sentence I want to display only 3 word from right side. I don't want to use jQuery or Javascript. I want to achive my output using php.

My Expected output should be like this: Demo post is incididunt

CodePudding user response:

You can split the string with a given delimiter string ('Demo') so that you have two distinct strings (left and right part). After that you just split the words of the second part (using the space as delimiter) and take a subset of that list with as many words as you expect to return (4).

I made a function getSubStringAfterDelimiter that will perform such an action with the freedom to choose: an arbitrary delimiter as the starting point; the number of words to pick after the delimiter; and if the returned string should also include the delimiter as well. Those arguments have a default value set so that by default you can call the function with a less verbose line.

https://onlinephp.io/c/93bb6

<?php

$input = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor Demo post is incididunt ut labore et dolore magna aliqua";
$output = getSubstringAfterDelimiter($input);
echo $output;

function getSubStringAfterDelimiter($subject, $delimiter = 'Demo', $nrOfWordsAfterDelimiter = 4, $includeDelimiter = true){
    //splits the string in two parts inside an array with 2 elements (left and right parts)
    $parts = explode(" ${delimiter} ", $subject);
    //separates the words contained in part2
    $wordsAfterDelimiter = explode(' ', $parts[1]);
    //takes a subset ($nrOfWordsAfterDelimiter) of words from part2
    $subset = array_slice($wordsAfterDelimiter, 0, $nrOfWordsAfterDelimiter);
    //joins together the words from $subset in a string
    $output = implode(' ', $subset);
    //if the flag $includeDelimiter is true
    if($includeDelimiter)
        //returns the string with the number of words expected starting from the delimiter including the delimiter itself
        return "${delimiter} $output";
    else
        //returns the string with the number of words expected starting from the delimiter without including the delimiter itself
        return $output;
}

The call here will return Demo post is incididunt ut including Demo and picking 4 words after that.

*As a side note, the function doesn't check if the inputs are valid so it may throw an error in case the delimiter is not found at all or if there are not enough words as specified after the delimiter.

CodePudding user response:

You say you want the 3 last words, but you give 3 consecutive words somewhere in the last sentence.

In case you want the last 3 words, I would explode the sentence, then select the last 3 words. For example:

$sentence = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor Demo post is incididunt ut labore et dolore magna aliqua.';

// Get words as array elements
$words = explode(' ', $sentence);
// Slice last 3 array elements
$selection = array_slice($words, -3, 3);

// Join the last 3 array elements with a space, to get a string in return.
echo join(' ', $selection);
  • Related