I have a rewriter function. I compare and replace the variables in the curly brackets with the ones in the text.
For example;
My variables are: [“Hello|Hi”,”How are you| What’s up”,”quick|rapid|swift|agile|brisk”]
Text: Hello, how are you?
Rewritten text: Hello, {How are you| what’s up}
Even if I add an extra word per sentence with str_replace, the first words do not change. Even if I change the first sentence to
“word_to_be_deleted Hello, how are you?” I still get the same result.
Sentences:
$sentences = ["Hello|Hi","How are you| What's up","quick|rapid|swift|agile|brisk"];
Rewrite function:
function rewriter($text) {
global $sentences;
$text = nl2br($text);
$text = str_replace(', ', ' , ', $text);
$text = str_replace(', ', ' , ', $text);
$text = str_replace('?', ' ? ', $text);
$text = str_replace('.', ' . ', $text);
$text = str_replace('<br />', ' <br /> word_to_be_deleted ', $text);
foreach ($sentences as $sentence) {
$exsentence = explode('|', trim($sentence));
$i = 0;
foreach ($exsentence as $sax) {
if (count($exsentence) > $i) {
// $lastsentences[trim($exsentence[$i])] = implode('|', array_diff($exsentence, array($exsentence[$i])));
$lastsentences[trim($exsentence[$i])] = implode('|', $exsentence);
}
$i++;
}
}
// uzunluğa göre array'i diz
array_multisort(array_map('strlen', array_keys($lastsentences)), SORT_DESC, $lastsentences);
foreach ($lastsentences as $key => $value) {
$text = preg_replace(sprintf('/{[^}]+}(*SKIP)(*F)|%s/i', preg_quote(' ' . $key . ' ', '/')), ' {<b style="color:red">' . $value . '</b>} ', ' ' . $text . ' ');
}
$text = str_replace(' | ', '|', $text);
$text = str_replace(' , ', ', ', $text);
$text = str_replace(' , ', ', ', $text);
$text = str_replace('. ', '. ', $text);
$text = str_replace(' .', '.', $text);
$text = str_replace(' .', '.', $text);
$text = str_replace('word_to_be_deleted', '', $text);
return html_entity_decode($text);
}
Headings that do not change even though the variable is defined: