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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:43:45+00:00 2026-05-17T20:43:45+00:00

Given a block of text, I need to parse it for the existing of

  • 0

Given a block of text, I need to parse it for the existing of a keyword. Then on the first appearance of the keyword, I need to wrap bold tags around it (if it doesn’t already have them), on the second appearance of the keyword, italics, and on the third, underline.

Example using the keyword “help“:

This is some text with the keyword “help”. If you can help, I really appreciate it. Thanks for the help! If there are any more occurrences of the keyword help, I’ll ignore them.

Would be rewritten to be…

This is some text with the keyword “<b>help</b>”. If you can <em>help</em>, I really appreciate it. Thanks for the <u>help</u>! If there are any more occurrences of the keyword help, I’ll ignore them.

  • 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-17T20:43:45+00:00Added an answer on May 17, 2026 at 8:43 pm

    I’m assuming you need a PHP based solution because you mentioned str_ireplace.

    You can do it using preg_replace_callback.
    This function is similar to preg_replace but calls a user-defined callback function whose return value will be used for replacement.

    To keep track of the occurrence number I’ve used a static variable in the callback function.

    $keyword = 'help';
    
    // the callback function
    function fun($matches)
    {
            static $count = 0;
    
            // switch on $count and later increment $count.
            switch($count++) {
                    case 0: return '<b>'.$matches[1].'</b>';   // 1st time..use bold
                    case 1: return '<em>'.$matches[1].'</em>'; 
                    case 2: return '<u>'.$matches[1].'</u>';
                    default: return $matches[1];              // don't change others.
            }
    }
    
    // search for keyword separated by word boundaries.
    // if present call the callback function.
    $text = preg_replace_callback("/\b($keyword)\b/","fun",$text);
    

    Code In Action

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

Sidebar

Related Questions

Using Nokogiri, I need to parse a block given: <div class=some_class> 12 AB /
Given a large text block from a WYSIWYG like: Lorem ipsum dolor sit amet,
I need to obtain the number of parameters a given block takes. For example:
Given the following XML 'template': <Request module=CRM call=list_service_features id={ID}> <block name=auth> <a name=username format=text>{USERNAME}</a>
Given any block of text, using the following as an example: Baseball is a
I need to convert lib_someString to <a href=someString>someString</a> inside a block of text using
Given a text block like yada yadayada yadayada yadayada yadayada yadayada yadayada yadayada yadayada
Given the following code, i see no more need for the finally block for
Is there a way to block a given change based on the user role?
given i have the following block of code (function(){ var mb = { abc:function(){

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.