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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:12:44+00:00 2026-05-15T12:12:44+00:00

How can I remove the first instance of a certain piece of html from

  • 0

How can I remove the first instance of a certain piece of html from a string.

I’m looking to remove

      </tr>
    </table></td>

  </tr

which is near the begining, but it also appears throughout the string.

I also need a way to do the same thing but the last instance of it.

Anyone know?

  • 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-15T12:12:45+00:00Added an answer on May 15, 2026 at 12:12 pm

    if you know roughly how close to the end of the string the substring that you want to replace is, you use substr_replace() with a negative $start and $length parameters, or you could just code up a function by hand to actually go through, find the last occurrence, then delete it. something like this (untested, written very quickly):

    function replace_last_occurrence($haystack, $needle) {
        $pos = 0;
        $last = 0;
    
        while($pos !== false) {
            $pos = strpos($haystack, $needle, $pos);
            $last = $pos;
        }
    
        substr_replace($haystack, "", $last, strlen($needle));
    }
    

    similar, for first occurrence

    function replace_first_occurrence($haystack, $needle) {        
        substr_replace($haystack, "", strpos($haystack, $needle, $pos), 
            strlen($needle));
    }
    

    you could also generalize this to replace the nth occurrence:

    function replace_nth_occurrence($haystack, $needle, $n) {
        $pos = 0;
        $last = 0;
    
        for($i = 0 ; $i <= $n ; $i++) {
            $pos = strpos($haystack, $needle, $pos);
            $last = $pos;
        }
    
        substr_replace($haystack, "", $last, strlen($needle));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to I can remove posts from a specific category from homepages in wordpress?
I am looking for an API that can remove bundle items added in a
I am wondering How i can remove a part if my string . For
I want to know is there any way that I can remove duplicates from
How would I remove a view from this dialog? I know I can remove
How can I use a variable to remove all instances of a substring from
If I remove the Schema definition from my XML, then my XSLT works, but
I'm certain this is a dumb newbie question, but this is the first time
how can i remove duplicates in nsarray. for instance my array contains following data.
One can remove all calls to printf() using #define printf . What if I

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.