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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:21:39+00:00 2026-06-09T12:21:39+00:00

I am looking for a way to replace all string looking alike in entire

  • 0

I am looking for a way to replace all string looking alike in entire page with their defined values

Please do not recommend me other methods of including language constants.

Strings like this :

[_HOME]

[_NEWS]

all of them are looking the same in [_*] part

Now the big issue is how to scan a HTML page and to replace the defined values .

One ways to parse the html page is to use DOMDocument and then pre_replace() it

but my main problem is writing a pattern for the replacement

$pattern = "/[_i]/";
$replacement= custom_lang("/i/");
$doc = new DOMDocument();
$htmlPage = $doc->loadHTML($html);

preg_replace($pattern, $replacement, $htmlPage);
  • 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-09T12:21:40+00:00Added an answer on June 9, 2026 at 12:21 pm

    In RegEx, [] are operators, so if you use them you need to escape them.

    Other problem with your expression is _* which will match Zero or more _. You need to replace it with some meaningful match, Like, _.* which will match _ and any other characters after that. SO your full expression becomes,

    /\[_.*?\]/

    Hey, why an ?, you might be tempted to ask: The reason being that it performs a non-greedy match. Like,

    [_foo] [_bar] is the query string then a greedy match shall return one match and give you the whole of it because your expression is fully valid for the string but a non-greedy match will get you two seperate matches. (More information)

    You might be better-off in being more constrictive, by having an _ followed by Capital letters. Like,

    /\[_[A-Z]+\]/

    Update: Using the matched strings and replacing them. To do so we use the concept called back-refrencing.

    Consider modifying the above expression, enclosing the string in parentheses, like, /\[_([A-Z]+)\]/

    Now in preg-replace arguments we can use the expression in parentheses by back-referencing them with $1. So what you can use is,

    preg_replce("/\[_([A-Z]+)\]/e", "my_wonderful_replacer('$1')", $html);
    

    Note: We needed the e modifier to treat the second parameter as PHP code. (More information)

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

Sidebar

Related Questions

I’m looking for a way to go through a string and replace all instances
I'ms looking for a way to replace my variables in a string by their
I am looking for a way to replace the SRC attribute in all IMG
I am looking for a way to create a string variable containing certain values
I'm looking for a way to replace all instances of a form: model->variable with
I am looking for a way to replace all occurrences of 'A' with 1,
I'm looking for a way to replace all but first occurrences of a group
I'm looking for a way to replace certain strings with an image. All strings
I'm looking for a way to find/replace links to images (within user-generated content) without
I'm looking for a way to make the search/replace function of VS IDE regex-aware,

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.