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

  • Home
  • SEARCH
  • 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 6140497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:07:31+00:00 2026-05-23T18:07:31+00:00

I’m looking to strip a string, $left from the left of another string, $right

  • 0

I’m looking to strip a string, $left from the left of another string, $right. Given:

$left  = 'alpha beta gamma';
$right = 'beta gamma delta';

The desired output would be:

string(6) " delta"

Now, I’ve sort of accomplished this. I’ve written a function, to achieve this exactly:

function strip_left_from_right($left, $right){
    for($i = 0, $m = strlen($left); $i <= $m; $i++){
        $needle = substr($left, -$i);
        if(substr($right, 0, strlen($needle)) == $needle){
            return substr($right, strlen($needle));
        }
    }
}

And it works fine. However I want it to be be “greedy“, using as much of the $left string as possible. For instance:

$left  = "foofoofoofoo";
$right = "foofoofoobar";

// desired output
string(3) "bar"

// actual output
string(9) "foofoobar"

Essentially my question is two-fold;

  1. What would be the best way to perform a “greedy” match? (given I continue with this code)

And probably more important;

  2. Is there a better (non-iterative, set of core functions used in tandem) way to accomplish this?


The solution I went with, thanks to @Yoshi for getting my brain in that direction:

function intersect_split($left, $right, $greedy = true){
    for($i = 0, $m = strlen($left); $i <= $m; $i++){
        $chunk = substr($left, $i * (int) ($greedy ?: -1));
        if(substr($right, 0, strlen($chunk)) == $chunk){
            return array(
                (string) substr($left, 0, $m - strlen($chunk)),
                (string) substr($right, strlen($chunk)),
            );
        }
    }
    return array($left, $right);
}

$left  = 'foo bar bar bar bar';
$right = 'bar bar bar bar baz';

var_dump( intersect_split($left, $right, true) );
var_dump( intersect_split($left, $right, false) );

Produces:

array(2) {
  [0]=>
  string(4) "foo "
  [1]=>
  string(4) " baz"
}
array(2) {
  [0]=>
  string(16) "foo bar bar bar "
  [1]=>
  string(16) " bar bar bar baz"
}

So now I essentially split the string at an intersecting match of the right of the $left argument and left of the $right argument, producing the leading and trailing strings in an array. $greedy produces the obvious difference in results.

  • 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-23T18:07:31+00:00Added an answer on May 23, 2026 at 6:07 pm

    Not optimal, but at least it does the job:

    function helper($left, $right) {
        $match = '';
    
        for ($i = strlen($left) - 1; $i >= 0; $i -= 1) {
            $chunk = substr($left, $i);
            $len = strlen($chunk);
            if (substr($right, 0, $len) == $chunk && $len > strlen($match)) {
                $match = $chunk;
            }
        }
    
        return substr($right, strlen($match));
    }
    
    echo helper('alpha beta gamma', 'beta gamma delta'); // output " delta"
    echo helper('foofoofoofoo', 'foofoofoobar'); // output "bar"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I have a jquery bug and I've been looking for hours now, I can't
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

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.