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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:00:24+00:00 2026-05-13T09:00:24+00:00

I have a question about preg_replace() function. I’m using it with ‘e’ modifier. Here

  • 0

I have a question about preg_replace() function. I’m using it with ‘e’ modifier. Here is code snippet:

$batchId = 2345;
$code = preg_replace("/[A-Za-z]{2,4}[\d\_]{1,5}[\.YRCc]{0,4}[\#\&\@\^]{0,2}/e",
                     'translate_indicator(\'$0\', {$batchId})', $code);

I want to have access to $batchId variable inside translate_indicator($code, $batch=false) function. The above exapmle, unfortunately, doesn’t work correctly: $batch is invisible(var_dump() result is bool(false)) within translate_indicator().

Probably, I have syntax mistakes in replacement code. Or, maybe, it’s impossible to pass variables with preg_replace()?

Update for the first two answers.

Thank you for answers, but your advice didn’t help. Besides I’ve already tried double qoutes instead of single qoutes. I’ve just simplified code to test possibility of passing parameter to the function:

$code = preg_replace("/[A-Za-z]{2,4}[\d\_]{1,5}[\.YRCc]{0,4}[\#\&\@\^]{0,2}/e",
                     "translate_indicator('$0', 12)", $code);

Also I’ve removed default value for the $batch within translate_indicator(). Result:

Warning: Missing argument 2 for translate_indicator()

So I think it’s impossible to pass parameter using this approach.:(

  • 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-13T09:00:24+00:00Added an answer on May 13, 2026 at 9:00 am
    $batchId = 2345;
    $code = 'AA1#';
    $code = preg_replace(
      "/[A-Za-z]{2,4}[\d\_]{1,5}[\.YRCc]{0,4}[\#\&\@\^]{0,2}/e",
      "translate_indicator('\$0', $batchId)", /* if $batchId can be a string use 'batchId' */
      $code);
    
    function translate_indicator($s, $batchId) {
      echo "translate_indicator($s, $batchId) invoked\n";
    }
    

    prints translate_indicator(AA1#, 2345) invoked.
    You can also use preg_replace_callback and a class/an object

    class Foo {
      public $batchId = 2345;
      public function translate_indicator($m) {
        echo "batchId=$this->batchId . $m[0]\n";
      }
    }
    
    $code = 'AA1#';
    $foo = new Foo;
    $code = preg_replace_callback(
      '/[A-Za-z]{2,4}[\d\_]{1,5}[\.YRCc]{0,4}[\#\&\@\^]{0,2}/',
      array($foo, 'translate_indicator'),
      $code
    );
    

    As of php 5.3. you can also use an anonymous function + closure to “pass” the additional parameter.

    $code = 'AA1#';
    $batchId = 2345;
    $code = preg_replace_callback(
      '/[A-Za-z]{2,4}[\d\_]{1,5}[\.YRCc]{0,4}[\#\&\@\^]{0,2}/',
      function($m) use ($batchId) {
        echo "batchid=$batchId . code=$m[0]\n";
      },
      $code
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have question about my code which does the Hoare Partition Method. Here is
I have a question about how GC works in Java. Consider the following code:
I have a quick question about a preg_replace problem I have. I am just
I have question about Dictionaries in Python. here it is: I have a dict
I'm a newbie and I have a very basic question about PHP arrays Code:
https://stackoverflow.com/a/64983/468251 - Hello, I have question about this code, how made that working with
I have question about parsing in Html helper : I have sth like: @foreach
I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass
I have question about XSLT1.0. The task is to write out in HTML all
I have question about normalization. Suppose I have an applications dealing with songs. First

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.