0

Good day.

We have code:

function testfunc($text){
 return $text;
}


$text = "Simple text {testfunc(1)} and next simple text and we {testfunc(20)} are happy ";
// number in {testfunc()} only for example - it can be other number or text.

Tell me please how replace {testfunc(1)} on result function testfunc($num)?

P.S.: In result need get next text:

$text = "Simple text 1 and next simple text and we 20 are happy ";

5 Answers5

1

Try this:

$newText = preg_replace_callback("/\{testfunc\((\d+)\)\}/","testfunc",$text);

And you'll get array of matches in your "testfunc". For what you want, this code:

I think, it helps you.

<?php

function testfunc($text){
    return $text[1];
}


$text = "Simple text {testfunc(1)} and next simple text and we {testfunc(20)} are happy ";
$newText = preg_replace_callback("/\{testfunc\((\d+)\)\}/","testfunc",$text);

echo $text."\n";
echo $newText."\n";
?>
BaBL86
  • 2,602
  • 1
  • 14
  • 13
0

This should work, using string concatenation:

$text = "Simple text " . testfunc(1) . " and next simple text and we " . testfunc(20) . " are happy ";

carl_h
  • 2,193
  • 1
  • 15
  • 19
0

Try

$text = "Simple text ".testfunc(1)." and next simple text and we ".testfunc(20)." are happy ";
VF_
  • 2,627
  • 17
  • 17
0

As normal why not try with,

$text = "Simple text ".testfunc(1)." and next simple text and we ".testfunc(20)." are happy ";
Jenz
  • 8,280
  • 7
  • 44
  • 77
-1
<?php
     $text = "Simple text ".testfunc(1)." and next simple text and we ".testfunc(20)." are
happy";
echo $text;
?>
Maulik patel
  • 2,546
  • 2
  • 20
  • 26