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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:22:23+00:00 2026-06-09T23:22:23+00:00

Possible Duplicate: How to parse and process HTML with PHP? PHP – Get part

  • 0

Possible Duplicate:
How to parse and process HTML with PHP?
PHP – Get part of string by searching for characters, instead of counting them?

I have a string:

$str = "hello world, this is mars"

and I want an improved strstr that will look like this:

istrstr($str, 'world', 'is')

and the return value will be:

"world, this"

In other words, there is a needle that starts and a needle that ends.

I was just wondering if there is a solution already, or I should just write one myself…

UPDATE:

based on the answers I did this function:

function istrstr($haystack, $needle_start, $needle_end, $include = false) {

    if (!$include) {
        $pos_start = strpos($haystack, $needle_start) + strlen($needle_start);
        $pos_end = strpos($haystack, $needle_end, $pos_start);
        return substr($haystack, $pos_start, $pos_end - $pos_start);
    }

}

for now I just need the excluding version, so I didn’t bother doing the including one…

  • 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-09T23:22:25+00:00Added an answer on June 9, 2026 at 11:22 pm
    function from_to($str, $from, $to) {
        return substr(
            $str,
            strpos($str, $from),
            strpos($str, $to) - strpos($str, $from) + strlen($to)
        );
    }
    

    This is basic string manipulation. Please read the manual a bit more.


    A more robust solution to close all edge cases (and includes documentation):

    <?php
    
    /**
     * @param string $string  The string to match against
     * @param string $from    Starting substring, from here
     * @param string $to      Ending substring, to here
     *
     * @return string         Substring containing all the letters from $from to $to inclusive.
     * @throws Exception      In case of $to being found before $from
     */
    function from_to($string, $from, $to) {
        //Calculate where each substring is found inside of $string
        $pos_from = strpos($string, $from);
        $pos_to   = strpos($string, $to);
    
        //The function will break if $to appears before $from, throw an exception.
        if ($pos_from > $pos_to) {
            throw new Exception("'$from' ($pos_from) appears before '$to' ($pos_to)");
        }
    
        return substr(
            $string,
            $pos_from, //From where the $from starts (first character of $from)
            $pos_to - $pos_from + strlen($to) //To where the $to ends. (last character of $to)
        );
    }
    
    $str = "hello world, and this not foo is mars";
    try {
        echo from_to($str, 'world', 'hell');
    }
    catch (Exception $e) {
        //In case 'hell' appeared before 'world'
        echo from_to($str, 'hell', 'world');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to parse and process HTML with PHP? i get page with
Possible Duplicate: How to parse and process HTML with PHP? I am brand new
Possible Duplicate: How to parse and process HTML with PHP? I have HTML document
Possible Duplicate: How to parse and process HTML with PHP? $content = <p>This is
Possible Duplicate: How to parse and process HTML with PHP? Here is what I'm
Possible Duplicate: How to parse and process HTML/XML with PHP? I want to grab
Possible Duplicate: How to parse HTML with PHP? Get MySQL database output via PHP
Possible Duplicate: How to parse and process HTML with PHP? I'm not very good
Possible Duplicate: How to parse and process HTML with PHP? I'm looking into HTML
Possible Duplicate: Best methods to parse HTML with PHP I wish to get the

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.