PHP preg_replace regex question

I need help finding a regex. In my script, I have a specific placeholder string. What I want to do is want each placeholder text to be a function that translates it to what it should be.

eg. my text:

Lorem ipsum dolor sits {{AMETPLACEHOLDER}} , consectetur adipiscing elit.

I want AMETPLACEHOLDER text sent to my function translateMe.

I am really bad at regex, but he tried anyway. I do not understand:

$sString    = preg_replace("(*.?)/\{{(*.?)}}(*.?)/", $this->echoText('\\2'), $sString);

Which course does not work.

Can anybody help me?

Br, Paul Pilen

+3
source share
2 answers

preg_replace_callback, , :

 = preg_replace_callback("@{{(.*?)}}@", array($this, "echoText"), $txt)

:

 public function echoText($match) {
     list($original, $placeholder) = $match;   // extract match groups
     ...
     return $translated;
 }

Btw, http://regular-expressions.info/ , : https://stackoverflow.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world

+5

/e, eval, preg_replace_callback().

.

$sString = preg_replace("#\{\{(*.?)\}\}#e", 'echoText("$2")', $sString);

$this , 5.3+, , , :

$sString = preg_replace_callback("#\{\{(*.?)\}\}#", array($this, 'echoText'), $sString);

$this->echoText() , , .

:

$sString = preg_replace_callback("#\{\{(*.?)\}\}#", function ($matches) {
               return $this->echoText($matches[1]);
           }, $sString);
+4

Source: https://habr.com/ru/post/1793181/


All Articles