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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:39:39+00:00 2026-05-21T09:39:39+00:00

I am relatively new to php, and hope someone can help me with a

  • 0

I am relatively new to php, and hope someone can help me with a replace regex, or maybe a match replace I am not exactly sure.

I want to automatically bold the (second occurance of a match) and then make the 4th appearance of a match italic and then the 7th appearance of a match underlined.

This is basically for SEO purposes in content.

I have done some replacements with: and were thinking this should do the trick?

preg_replace( pattern, replacement, subject [, limit ])

I already know the word I want to use in

'pattern' is also a word that is already defined like [word].
`replacement` 'This is a variable I am getting from a mysql db.
'subject' - The subject is text from a db.

Lets say I have this content: This explains more or less what I want to do.

This is an example of the text that I want to replace. In this text I want to make the second occurance of the word example < bold. Then I want to skip the next time example occurs in the text, and make the 4th time the word example appears in italic. Then I want to skip the 5th time the word example appears in the text, as well as the 6th time and lastly wants to make the 7th time example appears in the text underline it. In this example I have used a hyperlink as the underline example as I do not see an underline function in the text editor. The word example may appear more times in the text, but my only requerement is to underline once, make bold once and make italic once. I may later descide to do some quotes on the word “example” as well but it is not yet priority.

It is also important for the code not to through an error if there is not atleast 7 occurances of the word.

How would I do this, any ideas would be appreciated.

  • 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-21T09:39:40+00:00Added an answer on May 21, 2026 at 9:39 am

    You could use preg_split to split the text at the matches, apply the modifications, and then put everything back together:

    $parts = preg_split('/(example)/', $str, 7, PREG_SPLIT_DELIM_CAPTURE);
    if (isset($parts[3])) $parts[3] = '<b>'.$parts[3].'</b>';
    if (isset($parts[7])) $parts[7] = '<i>'.$parts[7].'</i>';
    if (isset($parts[13])) $parts[13] = '<u>'.$parts[13].'</u>';
    $str = implode('', $parts);
    

    The index formula for the i-th match is index = i · 2 – 1.

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

Sidebar

Related Questions

I'm relatively new to OOP in PHP, and I'm not sure if what I'm
This may be a silly question, but as someone relatively new to PHP, I'm
I'm relatively new to PHP, and I want to ensure I code as efficiently
I am relatively new to CodeIgniter, so I'm not sure if this is just
I am relatively new to PHP, but experienced Java programmer in complex enterprise environments
Note: I'm relatively new to Objective-C and am coming from Java and PHP. Could
Im relatively new to PHP but have realized it is a powerfull tool. So
i'm relatively new to php and mysql and would like to know how to
I am relatively new to php and have a feeling that I am going
I am relatively new to programming with PHP. I have created a simple Javascript

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.