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 8603337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:18:00+00:00 2026-06-12T02:18:00+00:00

this is what I try to get: My longest text to test When I

  • 0

this is what I try to get:
My longest text to test When I search for e.g. My I should get My longest

I tried it with this function to get first the complete length of the input and then I search for the ‘ ‘ to cut it.

$length = strripos($text, $input) + strlen($input)+2;

$stringpos = strripos($text, ' ', $length);

$newstring = substr($text, 0, strpos($text, ' ', $length));

But this only works first time and then it cuts after the current input, means
My lon is My longest and not My longest text.

How I must change this to get the right result, always getting the next word. Maybe I need a break, but I cannot find the right solution.

UPDATE

Here is my workaround till I find a better solution. As I said working with array functions does not work, since part words should work. So I extended my previous idea a bit. Basic idea is to differ between first time and the next. I improved the code a bit.

function  get_title($input, $text) {
    $length       = strripos($text, $input) + strlen($input);   
$stringpos = stripos($text, ' ', $length);
// Find next ' '
$stringpos2 = stripos($text, ' ', $stringpos+1);

if (!$stringpos) {
    $newstring = $text;
} else if ($stringpos2) {
    $newstring = substr($text, 0, $stringpos2);
}  }    

Not pretty, but hey it seems to work ^^. Anyway maybe someone of you have a better solution.

  • 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-06-12T02:18:01+00:00Added an answer on June 12, 2026 at 2:18 am

    You can try using explode

    $string = explode(" ", "My longest text to test");
    $key = array_search("My", $string);
    echo $string[$key] , " " , $string[$key + 1] ;
    

    You can take i to the next level using case insensitive with preg_match_all

    $string = "My longest text to test in my school that is very close to mY village" ;
    var_dump(__search("My",$string));
    

    Output

    array
      0 => string 'My longest' (length=10)
      1 => string 'my school' (length=9)
      2 => string 'mY village' (length=10)
    

    Function used

     function __search($search,$string)
     {
        $result = array();
        preg_match_all('/' . preg_quote($search) . '\s+\w+/i', $string, $result);
        return $result[0]; 
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to get this following url using the downloadURL function as follows: http://www.ncbi.nlm.nih.gov/nuccore/27884304
Somehow today when I input: try {} catch {} I get this: try {
i have been to this site to try and get my car movement sorted
I am trying to get twitter updates like this: try { $doc = new
I try to get string from this url: http://autoc.finance.yahoo.com/autoc?query=google&callback=YAHOO.Finance.SymbolSuggest.ssCallback When I look at this
I have a JSON which I get from web application. I try this parse
Try running this in a .VBS file MsgBox(545.14-544.94) You get a neat little answer
I get this error when i try to upload an image: OSError at /upload/
I get this RSpec error when I try to embed one document in another.
I get this error when I try to use time_ago_in_words : Comparison of String

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.