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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:36:16+00:00 2026-06-11T09:36:16+00:00

I’ve not yet mastered regex, so would appreciate your help with the code. I

  • 0

I’ve not yet mastered regex, so would appreciate your help with the code.

I need to replace all lines that:

  1. start with brackets or parentheses;
  2. which may contain either a regular number of up to 3 digits or a combination of up to 3 letters;
  3. which may be followed by a period;
  4. which digis or the numbers may or may not be inside or tags.

Here’s the example of what it needs to be replaced with:

(1)blahblah => %%(1)|blahblah
(<i>iv</i>.) blahblah => %%(<i>iv</i>.)|blahblah
[b] &nbsp;some stuff => %%[b]|&nbsp;some stuff

So the regex will need to recognize if it needs to be applied to the particular string, and if yes, put %% in the beginning of the line, then put the stuff inside the brackets, then put a pipe | (if there is a space between the brackets and the rest of the text, delete the space), and finally place the rest of the line.

So, let’s assume I have an array that I’m trying to run through the function that will either process the string (if it matches the criteria), or return it unchanged.

I only need to know how to write the function.

Thanks

  • 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-11T09:36:17+00:00Added an answer on June 11, 2026 at 9:36 am

    Here is my version.

    It uses the fallback regular expression if the first one doesn’t match (as agreed upon previously).

    Demo

    Code:

    <?php
    function do_replace($string) {
        $regex = '/^(\((?:<([a-z])>)?(\d{0,3}|[a-z]{1,3})(?:<\/\2>)?(\.)?\)|\[(?:<([a-z])>)?(\d{0,3}|[a-z]{1,3})(?:<\/\2>)?(\.)?\])\s*(.*)/i';
        $result = preg_match($regex, $string);
        if($result) {
            return preg_replace($regex, '%%$1|$8', $string);
        } else {
            $regex = '/^(\d{0,3}|[a-z]{1,3})\.\s*(.+)$/i';
            $result = preg_match($regex, $string);
            if($result) {
                return preg_replace($regex, '%%$1.|$2', $string);
            } else {
                return $string;
            }
        }
    }
    $strings = array(
        '(1)blahblah',
        '(<i>iv</i>.) blahblah',
        '[b] &nbsp;some stuff',
        '25. blahblah',
        'A. some other stuff. one',
        'blah. some other stuff',
        'text (1) text',
        '2008. blah',
        '[123) <-- mismatch'
    );
    foreach($strings as $string) echo do_replace($string) . PHP_EOL;
    ?>
    

    First regular expression expanded:

    $regex = '
        /
            ^(
                \(
                    (?:<([a-z])>)?
                    (
                        \d{0,3}
                        |
                        [a-z]{1,3}
                    )
                    (?:<\/\2>)?
                    (\.)?
                \)
                |
                \[
                    (?:<([a-z])>)?
                    (
                        \d{0,3}
                        |
                        [a-z]{1,3}
                    )
                    (?:<\/\2>)?
                    (\.)?
                \]
            )
            \s*
            (.*)
        /ix';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
In my XML file chapters tag has more chapter tag.i need to display chapters
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.