Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4562122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:10:17+00:00 2026-05-21T18:10:17+00:00

The code below takes a keyword and a string of text (sanitized of html

  • 0

The code below takes a keyword and a string of text (sanitized of html tags) and determines if the keyword appears in the last sentence of the sanitized content.

There’s one glitch that I can’t figure. When the end of the content contains so much as an empty space or paragraph tag with a nonbreaking space, ie

This is the last sentence.<p>&nbsp;</p>

I get a false negative (no match), despite the fact that (1) The keyword is definitely in the last sentence and (2) the strip_tags() function should render the appearance of the tag at the end a non issue.

Anyone see why that might be happening?

function plugin_get_kw_last_sentence($post) {
    $theContent = strip_tags(strtolower($post->post_content));
    $theKeyword = 'test';
    $thePiecesByKeyword = plugin_get_chunk_keyword($theKeyword,$theContent);
    if (count($thePiecesByKeyword)>0) {
        $theCount = $thePiecesByKeyword[count($thePiecesByKeyword)-1];
        $theCount = trim($theCount,'.');
        if (substr_count($theCount,'.')>0) {
            return FALSE;
        } else {
            return TRUE;
        }
    }
    return FALSE;
}

function plugin_get_chunk_keyword($theKeyword, $theContent) {
    if (!plugin_get_kw_in_content($theKeyword,$theContent)) {
        return array();
    }

    $myPieceReturn = preg_split('/\b' . $theKeyword . '\b/i', $theContent);
    return $myPieceReturn;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T18:10:17+00:00Added an answer on May 21, 2026 at 6:10 pm

    You’ve got a lot going on there that I think can be covered in the regex alone, if I understand your logic correctly. Couldn’t the whole logic be reduced down to this:

    function plugin_get_kw_last_sentence($post) {
        $pattern = '/' . $theKeyword . '[^.!?]*[.!?][^.!?]*$/';
        $subject = strip_tags(strtolower($post->post_content));
        return preg_match($pattern, $subject);
    }
    

    The regex matches when it finds your keyword and the final sentence ending punctuation mark with no other sentence ending punctuation marks between them.

    Now this is obviously not bullet proof as things like titles (i.e., Mr., Mrs.) and etc… and anything else including these sentence ending punctuation marks will throw you off. This should get you what you’re asking for, as your given code does not account for those situations either.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The piece of code below takes a string (userstring) and searches all .txt and
I'm using the code below to load a text file content into a WebView
The code below takes an array value, if it's key exist it should echo
the code below works fine but it takes an absolute age to run. How
The code below is designed to take a string in and remove any of
below content is taken from Best practice: Writing efficient code but i didn't understand
This code below takes in an two digit number (let's say 12) in ASCII
My code below takes a URL inputted by the user and prints out the
I have the code below running in my application and it takes approx 2
I have a bit of f# code (below) that takes a unit->obj and evaluates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.