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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:42:48+00:00 2026-05-12T15:42:48+00:00

I need a regex (to work in PHP) to replace American English words in

  • 0

I need a regex (to work in PHP) to replace American English words in HTML with British English words. So color would be replaced by colour, meters by metres and so on [I know that meters is also a British English word, but for the copy we’ll be using it will always be referring to units of distance rather than measuring devices]. The pattern would need to work accurately in the following (slightly contrived) examples (although as I have no control over the actual input these could exist):

<span style="color:red">This is the color red</span>

[should not replace color in the HTML tag but should replace it in the sentence]

<p>Color: red</p>

[should replace word]

<p>Tony Brammeter lives 2000 meters from his sister</p>

[should replace meters for the word but not in the name]

I know there are edge cases where replacement wouldn’t be useful (if his name was Tony Meter for example), but these are rare enough that we can deal with them when they come up.

  • 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-12T15:42:48+00:00Added an answer on May 12, 2026 at 3:42 pm

    Html/xml should not be processed with regular expressions, it is really hard to generate one that will match anything. But you can use the builtin dom extension and process your string recursively:

    # Warning: untested code!
    function process($node, $replaceRules) {
        foreach ($node->children as $childNode) {
            if ($childNode instanceof DOMTextNode) {
                $text = pre_replace(
                    array_keys(replaceRules),
                    array_values($replaceRules),
                    $childNode->wholeText
                );
                $node->replaceChild($childNode, new DOMTextNode($text));
            } else {
                process($childNode, $replaceRules);
            }
        }
    }
    $replaceRules = array(
        '/\bcolor\b/i' => 'colour',
        '/\bmeter\b/i' => 'metre',
    );
    $doc = new DOMDocument();
    $doc->loadHtml($htmlString);
    process($doc, $replaceRules);
    $htmlString = $doc->saveHTML();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a regex that will determine if a given SQL statement has a
I am having trouble getting a regex to work. This is the string. some
I've been struggling with finding a suitable solution :- I need an regex expression
I wrote a regex, but it doesn't work as i expect. Take a look
I'm writing a php forms class with client and server side validation. I'm having
I am trying to create a Javascript Regex that captures the filename without the
I've spent some time, but still have to solution. I need regular expression that
I need to find, process and remove (one by one) any substrings that match
I'm working on the search box for an events website. I've been recording the
I am not a professional web developer but I love web development as a

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.