Можно также подойти к вашей проблеме, используя массив ключей для изменения, как показано ниже. Это может быть полезно при попытке заменить несколько строк.
<?php
// array with keys that you'll be changing in your text
$toChange = array(
"{toChangeOne}" => "First Change",
"{toChangeTwo}" => "Second Change",
"{toChangeThree}" => "Third Change"
);
$fileContent = '<div class="panel-body">
{toChangeOne}{toChangeTwo}
{
toChangeThree
}
</div>';
// loop through all the keys you want to change
foreach($toChange as $key => $value){
// prep regex
// remove the openning and curly braces this
// way we can match anything that matches our
// keys even if there's a mixture of returns
// or empty spaces within the curly braces
$key_text = str_replace("{", "", $key);
$key_text = str_replace("}", "", $key_text);
// "\{" - matches the character "{" literally
// "(\s|\s+)?" - matches any white space. In our case
// we might want it to be optional hense
// the "?"
// "\}" - matches the character "}" literally
$regex = '/\{(\s|\s+)?'.$key_text.'(\s|\s+)?\}/';
$fileContent = preg_replace($regex, $value, $fileContent);
}
echo $fileContent;
И что вы хотите сделать сейчас? Я знаю только, что вы хотите что-то заменить. что ты уже испробовал? –
Вы также хотите заменить фактические фигурные скобки? Или оставить фигурные скобки и заменить текст? – Quixrick
Да также замените фигурные скобки – Olympus