<?php
if ( ! defined('PHP_EOL')) {
define('PHP_EOL', chr(10));
}
$string = 'Hello {{ name }}, nice to meet you.';
define('TAG_OPEN', '{{');
define('TAG_CLOSE', '}}');
define('STATE_TEXT', 'STATE_TEXT');
define('STATE_IN_TAG', 'STATE_IN_TAG');
function isOpenTag($string, $index) {
return TAG_OPEN === substr($string, $index, strlen(TAG_OPEN));
}
function isCloseTag($string, $index) {
return TAG_CLOSE === substr($string, $index, strlen(TAG_CLOSE));
}
function isText($string, $index) {
return ! (isOpenTag($string, $index) || isCloseTag($string, $index));
}
$state = STATE_TEXT;
for($index = 0; $index < strlen($string); $index++)
{
if (isText($string, $index) && $state != STATE_IN_TAG) {
continue;
}
if (isOpenTag($string, $index)) {
$state = STATE_IN_TAG;
$index += strlen(TAG_OPEN) - 1;
continue;
}
if (isText($string, $index) && $state = STATE_IN_TAG) {
echo $index, ' ', $string[$index], PHP_EOL;
}
if (isCloseTag($string, $index)) {
$state = STATE_TEXT;
$index += strlen(TAG_CLOSE) - 1;
continue;
}
}
preferences:
64.1 ms | 402 KiB | 5 Q