I have the following HTML that's been returned from a function:
<fieldset>
<legend>Title</legend>
<div>
<label>
<i>String that gets translated</i>
</label>
<textarea>
<i>Another string</i>
</textarea>
</div>
</fieldset>
Then I use preg_replace_callback to get the string between <i> tags and I replace it with a translated string, like below:
$translation = preg_replace_callback("/\<i\>(.+?)\<\/i\>/", 'translator', $html);
function translator($matches) {
return __t($matches[1]);
}
However, when I output the html - echo $translation; - I get the following:
String that gets translated Another string<--this is not inside <i> tags
<fieldset>
<legend>Title</legend>
<div>
<label>
<i></i> <--the string should be placed here
</label>
<textarea>
<i></i> <--and here
</textarea>
</div>
</fieldset>
This issue has been puzzling my head all day and I can't figure out a way to sort it out. How can I output the html and the translated string in the right order? Do I have to use DOMDocument::loadHTML, and if so how?
echo __t($matches[1]);echothe__t($matches[1])alone. They have to be echoed with the rest of the html. Why is this happening though? I think it's because__t()function echoes the string and not returning it. How can I prevent this? I can't edit the__t()function though.t()function echoing and not returning the string: 3v4l.org/HoprL__tfunction not returning then its the problem..ob_XXXfunctions to capture the output buffer into a string.