- preg_replace: documentation ( source)
<?php
$html = <<<HTML
<figure data-trix-attachment="{"content":"<span class=\"trix-token\" data-token-id=\"landlord_name\">Jméno pronajímatele</span>"}" data-trix-content-type="undefined" class="attachment attachment--content"><span class="trix-token">Jméno pronajímatele</span><figcaption class="attachment__caption"></figcaption></figure>
HTML;
$tokenId = 'landlord_name';
// Správný regulární výraz
$figurePattern = '/(<figure[^>]*data-trix-attachment="[^"]*data-token-id=\\"' . $tokenId . '\\".*?"[^>]*>)(.*?)<\/figure>/s';
// Náhrada obsahu
$replacement = '$1<span class="trix-token ' . $tokenId . ' tokenClass" data-token-id="' . $tokenId . '">test</span></figure>';
// Proveď nahrazení
$html = preg_replace($figurePattern, $replacement, $html);
echo $html;