3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getCopiedText($model, $answer, $min = 4) { //copy original answerstr $modelOriginal = $answer; //replace all space characters from model with single white-space $model = preg_replace('/\s+/iu', ' ', $model); //ensure there are no double spaces in the answer. $answer = preg_replace('/\s+/iu', ' ', $answer); $test = new \CachingIterator(new \ArrayIterator(explode(' ', $answer))); $words = $matches = []; $p = $match = null; //test each word foreach($test as $i => $word) { $words[] = $word; $count = count($words); if ($count === 2) { //save pointer at second word $p = $i; } //check if the phrase of words exists in the model if (false !== stripos($model, $phrase = implode(' ', $words))) { //only match phrases with the minimum or more words if ($count >= $min) { //reset back to here for more matches $match = $phrase; if (!$test->hasNext()) { //add the the last word to the phrase $matches[$match] = true; $p = null; } } } else { //the phrase of words was no longer found if (null !== $match && !isset($matches[$match])) { //add the matched phrase to the list of matches $matches[$match] = true; $p = null; $iterator = $test->getInnerIterator(); if ($iterator->valid()) { //rewind pointer back to the current word since the current word may be part of the next phrase $iterator->seek($i); } } elseif (null !== $p) { //match not found, determine if we need to rewind the pointer $iterator = $test->getInnerIterator(); if ($iterator->valid()) { //rewind pointer back to second word since a partial phrase less than 4 words was matched $iterator->seek($p); } $p = null; } //reset testing $words = []; $match = null; } } //highlight the matched phrases in the answer if (!empty($matches)) { $phrases = array_keys($matches); //sort phrases by the length array_multisort(array_map('strlen', $phrases), $phrases); //filter the matches as regular expression patterns //order by longest phrase first to ensure double highlighting of smaller phrases $phrases = array_map(function($phrase) { return '/(' . preg_quote($phrase, '/') . ')/iu'; }, array_reverse($phrases)); $modelOriginal = preg_replace($phrases, '<span style="color:red">$0</span>', $modelOriginal); } return $modelOriginal; } $model ="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; $answer = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; echo getCopiedText($model,$answer);

preferences:
33.32 ms | 416 KiB | 6 Q