How to get certain words from a text with full sentences in PHP?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I want to get some sentences from a text.



Sample text is following,




Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.




What I've done so far is I'm able to get 30 words from a large text but at the end, I've got an incomplete sentence and I want to remove such sentence.



Here is the function to get 30 words,



/**
* @param $sentence
* @param int $count
* @return mixed
*/
function get_words($sentence, $count = 30) {
preg_match("/(?:w+(?:W+|$)){0,$count}/", $sentence, $matches);
return $matches[0];
}


I've used above function from the question below



How to select first 10 words of a sentence?



When I pass above text to the function I've got output like this,




Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a




Here the last sentence is incomplete and I don't want such in my output.



Is there any way to achieve this?



I'm working with PHP and Laravel any kind of help and suggestions are appreciated.










share|improve this question

























  • What you're trying to do is pretty complex, I would say - unless you expect some kind of recurrence of the incomplete sentences in the input? Otherwise, I would suggest taking a look at Natural Language Processing software (Spacy is a fast example) - that type of software can help you to dissect these sentences, get tokens and determine if there's enough in a sentence to be a full sentence.

    – T. Altena
    Jan 3 at 7:36











  • @T.Altena Thanks, for your response. I also thought that but for not so important task implementing NLP is tough one. I think so. I'm looking for some programming tweak that i can achieve quite similar that

    – Sagar Gautam
    Jan 3 at 7:38











  • You can look at the answers below for Regex approximations of sentence endings. Beware though - if you are going to examine punctuation, abbreviations might throw you off (Prof. X said Y... ), and not all sentences end with a dot !

    – T. Altena
    Jan 3 at 7:43











  • @T.Altena I've got one solution for my case

    – Sagar Gautam
    Jan 3 at 7:46


















1















I want to get some sentences from a text.



Sample text is following,




Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.




What I've done so far is I'm able to get 30 words from a large text but at the end, I've got an incomplete sentence and I want to remove such sentence.



Here is the function to get 30 words,



/**
* @param $sentence
* @param int $count
* @return mixed
*/
function get_words($sentence, $count = 30) {
preg_match("/(?:w+(?:W+|$)){0,$count}/", $sentence, $matches);
return $matches[0];
}


I've used above function from the question below



How to select first 10 words of a sentence?



When I pass above text to the function I've got output like this,




Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a




Here the last sentence is incomplete and I don't want such in my output.



Is there any way to achieve this?



I'm working with PHP and Laravel any kind of help and suggestions are appreciated.










share|improve this question

























  • What you're trying to do is pretty complex, I would say - unless you expect some kind of recurrence of the incomplete sentences in the input? Otherwise, I would suggest taking a look at Natural Language Processing software (Spacy is a fast example) - that type of software can help you to dissect these sentences, get tokens and determine if there's enough in a sentence to be a full sentence.

    – T. Altena
    Jan 3 at 7:36











  • @T.Altena Thanks, for your response. I also thought that but for not so important task implementing NLP is tough one. I think so. I'm looking for some programming tweak that i can achieve quite similar that

    – Sagar Gautam
    Jan 3 at 7:38











  • You can look at the answers below for Regex approximations of sentence endings. Beware though - if you are going to examine punctuation, abbreviations might throw you off (Prof. X said Y... ), and not all sentences end with a dot !

    – T. Altena
    Jan 3 at 7:43











  • @T.Altena I've got one solution for my case

    – Sagar Gautam
    Jan 3 at 7:46














1












1








1








I want to get some sentences from a text.



Sample text is following,




Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.




What I've done so far is I'm able to get 30 words from a large text but at the end, I've got an incomplete sentence and I want to remove such sentence.



Here is the function to get 30 words,



/**
* @param $sentence
* @param int $count
* @return mixed
*/
function get_words($sentence, $count = 30) {
preg_match("/(?:w+(?:W+|$)){0,$count}/", $sentence, $matches);
return $matches[0];
}


I've used above function from the question below



How to select first 10 words of a sentence?



When I pass above text to the function I've got output like this,




Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a




Here the last sentence is incomplete and I don't want such in my output.



Is there any way to achieve this?



I'm working with PHP and Laravel any kind of help and suggestions are appreciated.










share|improve this question
















I want to get some sentences from a text.



Sample text is following,




Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.




What I've done so far is I'm able to get 30 words from a large text but at the end, I've got an incomplete sentence and I want to remove such sentence.



Here is the function to get 30 words,



/**
* @param $sentence
* @param int $count
* @return mixed
*/
function get_words($sentence, $count = 30) {
preg_match("/(?:w+(?:W+|$)){0,$count}/", $sentence, $matches);
return $matches[0];
}


I've used above function from the question below



How to select first 10 words of a sentence?



When I pass above text to the function I've got output like this,




Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a




Here the last sentence is incomplete and I don't want such in my output.



Is there any way to achieve this?



I'm working with PHP and Laravel any kind of help and suggestions are appreciated.







php string laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 7:15









Sayed Mohd Ali

1,5202520




1,5202520










asked Jan 3 at 6:32









Sagar GautamSagar Gautam

4,37431644




4,37431644













  • What you're trying to do is pretty complex, I would say - unless you expect some kind of recurrence of the incomplete sentences in the input? Otherwise, I would suggest taking a look at Natural Language Processing software (Spacy is a fast example) - that type of software can help you to dissect these sentences, get tokens and determine if there's enough in a sentence to be a full sentence.

    – T. Altena
    Jan 3 at 7:36











  • @T.Altena Thanks, for your response. I also thought that but for not so important task implementing NLP is tough one. I think so. I'm looking for some programming tweak that i can achieve quite similar that

    – Sagar Gautam
    Jan 3 at 7:38











  • You can look at the answers below for Regex approximations of sentence endings. Beware though - if you are going to examine punctuation, abbreviations might throw you off (Prof. X said Y... ), and not all sentences end with a dot !

    – T. Altena
    Jan 3 at 7:43











  • @T.Altena I've got one solution for my case

    – Sagar Gautam
    Jan 3 at 7:46



















  • What you're trying to do is pretty complex, I would say - unless you expect some kind of recurrence of the incomplete sentences in the input? Otherwise, I would suggest taking a look at Natural Language Processing software (Spacy is a fast example) - that type of software can help you to dissect these sentences, get tokens and determine if there's enough in a sentence to be a full sentence.

    – T. Altena
    Jan 3 at 7:36











  • @T.Altena Thanks, for your response. I also thought that but for not so important task implementing NLP is tough one. I think so. I'm looking for some programming tweak that i can achieve quite similar that

    – Sagar Gautam
    Jan 3 at 7:38











  • You can look at the answers below for Regex approximations of sentence endings. Beware though - if you are going to examine punctuation, abbreviations might throw you off (Prof. X said Y... ), and not all sentences end with a dot !

    – T. Altena
    Jan 3 at 7:43











  • @T.Altena I've got one solution for my case

    – Sagar Gautam
    Jan 3 at 7:46

















What you're trying to do is pretty complex, I would say - unless you expect some kind of recurrence of the incomplete sentences in the input? Otherwise, I would suggest taking a look at Natural Language Processing software (Spacy is a fast example) - that type of software can help you to dissect these sentences, get tokens and determine if there's enough in a sentence to be a full sentence.

– T. Altena
Jan 3 at 7:36





What you're trying to do is pretty complex, I would say - unless you expect some kind of recurrence of the incomplete sentences in the input? Otherwise, I would suggest taking a look at Natural Language Processing software (Spacy is a fast example) - that type of software can help you to dissect these sentences, get tokens and determine if there's enough in a sentence to be a full sentence.

– T. Altena
Jan 3 at 7:36













@T.Altena Thanks, for your response. I also thought that but for not so important task implementing NLP is tough one. I think so. I'm looking for some programming tweak that i can achieve quite similar that

– Sagar Gautam
Jan 3 at 7:38





@T.Altena Thanks, for your response. I also thought that but for not so important task implementing NLP is tough one. I think so. I'm looking for some programming tweak that i can achieve quite similar that

– Sagar Gautam
Jan 3 at 7:38













You can look at the answers below for Regex approximations of sentence endings. Beware though - if you are going to examine punctuation, abbreviations might throw you off (Prof. X said Y... ), and not all sentences end with a dot !

– T. Altena
Jan 3 at 7:43





You can look at the answers below for Regex approximations of sentence endings. Beware though - if you are going to examine punctuation, abbreviations might throw you off (Prof. X said Y... ), and not all sentences end with a dot !

– T. Altena
Jan 3 at 7:43













@T.Altena I've got one solution for my case

– Sagar Gautam
Jan 3 at 7:46





@T.Altena I've got one solution for my case

– Sagar Gautam
Jan 3 at 7:46












2 Answers
2






active

oldest

votes


















1














This below code may help you.



<?php
$sen="Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.";
$cropped_data = get_words($sen);
$strlength = strlen ( $cropped_data );
$remains= complete_sentence(substr($sen,$strlength));

function complete_sentence($content) {
$pos = strpos($content, '.');
return substr($content, 0, $pos+1);
}

function get_words($sentence, $count = 30) {
preg_match("/(?:w+(?:W+|$)){0,$count}/", $sentence, $matches);
return $matches[0];
}

echo "complete sentence<br/>".$cropped_data.$remains;
?>


Thanks.






share|improve this answer


























  • Works perfectly as I want. Thanks

    – Sagar Gautam
    Jan 3 at 7:42











  • while calling complete_sentence(), only resulted $cropped_data should be sent again, rather than $sen in substr's first parameter, else if . exists in a $sen after count of 30 words, it will return result up to that point. Resulting finally in more than 30 count of words. Also there will be no benefit of evaluating $cropped_data if it is not used proceeding forward. I have made an edit to answer, I hope it gets accepted.

    – Anant
    Jan 3 at 7:46





















0














function get_words($sentence, $count = 30) {

preg_match("/(?:w+(?:W+|$)){0,$count}/", $sentence, $matches);

return $matches[0];
};

$sentence = "Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.";

$cropSentence = get_words($sentence);

$finalSentence= substr($cropSentence, 0, strrpos($cropSentence, "."));
echo $finalSentence;


It will return until the last occurrence of (.);




Gryphon interrupted in a low voice. 'Not at all,' said the Dodo,
pointing to the confused clamour of the wood--(she considered him to
you, Though







share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017380%2fhow-to-get-certain-words-from-a-text-with-full-sentences-in-php%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    This below code may help you.



    <?php
    $sen="Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.";
    $cropped_data = get_words($sen);
    $strlength = strlen ( $cropped_data );
    $remains= complete_sentence(substr($sen,$strlength));

    function complete_sentence($content) {
    $pos = strpos($content, '.');
    return substr($content, 0, $pos+1);
    }

    function get_words($sentence, $count = 30) {
    preg_match("/(?:w+(?:W+|$)){0,$count}/", $sentence, $matches);
    return $matches[0];
    }

    echo "complete sentence<br/>".$cropped_data.$remains;
    ?>


    Thanks.






    share|improve this answer


























    • Works perfectly as I want. Thanks

      – Sagar Gautam
      Jan 3 at 7:42











    • while calling complete_sentence(), only resulted $cropped_data should be sent again, rather than $sen in substr's first parameter, else if . exists in a $sen after count of 30 words, it will return result up to that point. Resulting finally in more than 30 count of words. Also there will be no benefit of evaluating $cropped_data if it is not used proceeding forward. I have made an edit to answer, I hope it gets accepted.

      – Anant
      Jan 3 at 7:46


















    1














    This below code may help you.



    <?php
    $sen="Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.";
    $cropped_data = get_words($sen);
    $strlength = strlen ( $cropped_data );
    $remains= complete_sentence(substr($sen,$strlength));

    function complete_sentence($content) {
    $pos = strpos($content, '.');
    return substr($content, 0, $pos+1);
    }

    function get_words($sentence, $count = 30) {
    preg_match("/(?:w+(?:W+|$)){0,$count}/", $sentence, $matches);
    return $matches[0];
    }

    echo "complete sentence<br/>".$cropped_data.$remains;
    ?>


    Thanks.






    share|improve this answer


























    • Works perfectly as I want. Thanks

      – Sagar Gautam
      Jan 3 at 7:42











    • while calling complete_sentence(), only resulted $cropped_data should be sent again, rather than $sen in substr's first parameter, else if . exists in a $sen after count of 30 words, it will return result up to that point. Resulting finally in more than 30 count of words. Also there will be no benefit of evaluating $cropped_data if it is not used proceeding forward. I have made an edit to answer, I hope it gets accepted.

      – Anant
      Jan 3 at 7:46
















    1












    1








    1







    This below code may help you.



    <?php
    $sen="Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.";
    $cropped_data = get_words($sen);
    $strlength = strlen ( $cropped_data );
    $remains= complete_sentence(substr($sen,$strlength));

    function complete_sentence($content) {
    $pos = strpos($content, '.');
    return substr($content, 0, $pos+1);
    }

    function get_words($sentence, $count = 30) {
    preg_match("/(?:w+(?:W+|$)){0,$count}/", $sentence, $matches);
    return $matches[0];
    }

    echo "complete sentence<br/>".$cropped_data.$remains;
    ?>


    Thanks.






    share|improve this answer















    This below code may help you.



    <?php
    $sen="Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.";
    $cropped_data = get_words($sen);
    $strlength = strlen ( $cropped_data );
    $remains= complete_sentence(substr($sen,$strlength));

    function complete_sentence($content) {
    $pos = strpos($content, '.');
    return substr($content, 0, $pos+1);
    }

    function get_words($sentence, $count = 30) {
    preg_match("/(?:w+(?:W+|$)){0,$count}/", $sentence, $matches);
    return $matches[0];
    }

    echo "complete sentence<br/>".$cropped_data.$remains;
    ?>


    Thanks.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 3 at 7:33

























    answered Jan 3 at 7:24









    RamyaRamya

    1447




    1447













    • Works perfectly as I want. Thanks

      – Sagar Gautam
      Jan 3 at 7:42











    • while calling complete_sentence(), only resulted $cropped_data should be sent again, rather than $sen in substr's first parameter, else if . exists in a $sen after count of 30 words, it will return result up to that point. Resulting finally in more than 30 count of words. Also there will be no benefit of evaluating $cropped_data if it is not used proceeding forward. I have made an edit to answer, I hope it gets accepted.

      – Anant
      Jan 3 at 7:46





















    • Works perfectly as I want. Thanks

      – Sagar Gautam
      Jan 3 at 7:42











    • while calling complete_sentence(), only resulted $cropped_data should be sent again, rather than $sen in substr's first parameter, else if . exists in a $sen after count of 30 words, it will return result up to that point. Resulting finally in more than 30 count of words. Also there will be no benefit of evaluating $cropped_data if it is not used proceeding forward. I have made an edit to answer, I hope it gets accepted.

      – Anant
      Jan 3 at 7:46



















    Works perfectly as I want. Thanks

    – Sagar Gautam
    Jan 3 at 7:42





    Works perfectly as I want. Thanks

    – Sagar Gautam
    Jan 3 at 7:42













    while calling complete_sentence(), only resulted $cropped_data should be sent again, rather than $sen in substr's first parameter, else if . exists in a $sen after count of 30 words, it will return result up to that point. Resulting finally in more than 30 count of words. Also there will be no benefit of evaluating $cropped_data if it is not used proceeding forward. I have made an edit to answer, I hope it gets accepted.

    – Anant
    Jan 3 at 7:46







    while calling complete_sentence(), only resulted $cropped_data should be sent again, rather than $sen in substr's first parameter, else if . exists in a $sen after count of 30 words, it will return result up to that point. Resulting finally in more than 30 count of words. Also there will be no benefit of evaluating $cropped_data if it is not used proceeding forward. I have made an edit to answer, I hope it gets accepted.

    – Anant
    Jan 3 at 7:46















    0














    function get_words($sentence, $count = 30) {

    preg_match("/(?:w+(?:W+|$)){0,$count}/", $sentence, $matches);

    return $matches[0];
    };

    $sentence = "Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.";

    $cropSentence = get_words($sentence);

    $finalSentence= substr($cropSentence, 0, strrpos($cropSentence, "."));
    echo $finalSentence;


    It will return until the last occurrence of (.);




    Gryphon interrupted in a low voice. 'Not at all,' said the Dodo,
    pointing to the confused clamour of the wood--(she considered him to
    you, Though







    share|improve this answer




























      0














      function get_words($sentence, $count = 30) {

      preg_match("/(?:w+(?:W+|$)){0,$count}/", $sentence, $matches);

      return $matches[0];
      };

      $sentence = "Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.";

      $cropSentence = get_words($sentence);

      $finalSentence= substr($cropSentence, 0, strrpos($cropSentence, "."));
      echo $finalSentence;


      It will return until the last occurrence of (.);




      Gryphon interrupted in a low voice. 'Not at all,' said the Dodo,
      pointing to the confused clamour of the wood--(she considered him to
      you, Though







      share|improve this answer


























        0












        0








        0







        function get_words($sentence, $count = 30) {

        preg_match("/(?:w+(?:W+|$)){0,$count}/", $sentence, $matches);

        return $matches[0];
        };

        $sentence = "Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.";

        $cropSentence = get_words($sentence);

        $finalSentence= substr($cropSentence, 0, strrpos($cropSentence, "."));
        echo $finalSentence;


        It will return until the last occurrence of (.);




        Gryphon interrupted in a low voice. 'Not at all,' said the Dodo,
        pointing to the confused clamour of the wood--(she considered him to
        you, Though







        share|improve this answer













        function get_words($sentence, $count = 30) {

        preg_match("/(?:w+(?:W+|$)){0,$count}/", $sentence, $matches);

        return $matches[0];
        };

        $sentence = "Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.Gryphon interrupted in a low voice. 'Not at all,' said the Dodo, pointing to the confused clamour of the wood--(she considered him to you, Though.";

        $cropSentence = get_words($sentence);

        $finalSentence= substr($cropSentence, 0, strrpos($cropSentence, "."));
        echo $finalSentence;


        It will return until the last occurrence of (.);




        Gryphon interrupted in a low voice. 'Not at all,' said the Dodo,
        pointing to the confused clamour of the wood--(she considered him to
        you, Though








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 7:40









        Md.Sukel AliMd.Sukel Ali

        1,4681918




        1,4681918






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017380%2fhow-to-get-certain-words-from-a-text-with-full-sentences-in-php%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules