Ну, вы можете создать два массива, один со строками имени поля [[field-name]]
и один с ответами $school->get('field-name')
. Затем добавьте их в str_replace
, так как он поддерживает массивы.
Пример из руководства по PHP:
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);
// Resulting String: "You should eat pizza, beer, and ice cream every day."
Если вы все еще хотите реализовать свое предложение (найти все [[]] и заменить их), я постараюсь написать быструю функцию.
Редактировать: Вот два способа сделать это с помощью вашего запроса:
$html = "Hello, [[FirstName]]! Welcome to [[SiteName]].";
$count = preg_match_all("/\[\[([\w]+)\]\]/", $html, $matches);
for ($x = 0; $x < $count; $x++)
$html = str_replace($matches[0][$x], $school->get($matches[1][$x]), $html);
Или используя массивы:
$html = "Hello, [[FirstName]]! Welcome to [[SiteName]].";
$count = preg_match_all("/\[\[([\w]+)\]\]/", $html, $matches);
for ($x = 0; $x < $count; $x++)
$matches[1][$x] = $school->get($matches[1][$x]);
$html = str_replace($matches[0], $matches[1], $html);