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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:00:51+00:00 2026-06-14T14:00:51+00:00

I want to replace the second h2 tag to h3 and I hope someone

  • 0

I want to replace the second h2 tag to h3 and I hope someone can help me with a replacement regex, or maybe a preg_split – I am not exactly sure.

For example, this:

<h2>My text one</h2>
<h2>My text two</h2>
text …
<h2>My text three</h2>

Should become this:

<h2>My text one</h2>
<h3>My text two</h3>
text …
<h2>My text three</h2>
  • 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-14T14:00:53+00:00Added an answer on June 14, 2026 at 2:00 pm

    I agree with the other commentaries, this should be done through a dom parser. But here is a working php solution nevertheless.

    <?php 
         // Fill $str with the html;
    
         preg_replace("/[h]{1}[2]/i", "h3", $str);
    ?>
    

    or

    <?php
         // Fill $str with the html;
    
         str_replace("h2", "h3", $str);      
    ?>
    

    This should work just fine. Adding the $matches parameter to preg_replace will also keep track of the number of changes made.
    Now, using a loop you may control which element needs to be replaced, however, the functions as written above will detect all occurences of h2.

    Also, I’ve overly complicated the regexp for you to be able to swap out the number, to make a more useful function with it. Just using “/(h2)/i” will do the trick too.

    So, your code should implement a loop in the correct manner to prevent replacing all of the tags and you should decide if the function is going to handle just h2 or if it should be more flexible.

    As a final remark, str_replace is faster than preg_replace, so if this is the only edit you need to make, I would recommend str_replace.

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

Sidebar

Related Questions

I want to replace the second line file content, can somebody help please based
I want to replace a regex with '*', but only if the regex is
I want to replace IP address after second comma to 92.2.11.1 for all: pr,th-klmndisj,92.2.11.1
I have two-dimensional array and I want replace second of two-dimensional array on random
I want to replace \n with <br /> in a string. But not that
I want replace a(or each) element(number) instead another element(number) in a class while running
I want to replace some headers by images, so I would have nice font.For
i want to replace the bottom panel where we show Next,Back buttons with a
I want to replace part of the following html text (excerpt of a huge
I want to replace all html codes to empty space. I think I should

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.