Проигнорируйте пунктуацию и выделите образец в данной строке - PullRequest
0 голосов
/ 11 июля 2019

У меня есть одна строка модели и список соответствующих шаблонов. Я хочу выделить весь соответствующий шаблон в заданной строке модели, даже если любое слово в шаблоне / модели содержит знак пунктуации.

Пример строки:

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.

Список шаблонов: 1. printing and typesetting industry Lorem Ipsum 2. industry's standard dummy text ever since the 1500s, 3. type specimen book, It has survived 4. but also the leap into electronic typesetting, remaining essentially unchanged. 5. containing Lorem Ipsum passages and 6. PageMaker including versions of Lorem Ipsum.

Ожидаемый результат: expectedoutput

Что я получаю на выходе: enter image description here

Проблема:

Здесь шаблон 1,3,5 не выделяется. Потому что они содержат какой-то знак пунктуации, но знак пунктуации в модели для этого слова отсутствует.

# 1: В первом шаблоне после слова industry нет знака пунктуации, вместо строки модели в industry.. Кажется, что оба слова разные, поэтому это не выделение. Но я хочу, чтобы он игнорировал знак препинания и выделял строку.

# 3: в третьем шаблоне слово имеет различную пунктуацию book, и book.

Я хочу, чтобы выделил строку, даже если в строке модели или образца присутствует какое-либо слово, имеющее знак пунктуации.

Я не хочу никаких изменений в строке модели, они должны быть такими же, как и с пунктуацией, просто выделите соответствующий шаблон.

<?php
$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.';
$phrases= [
    "printing and typesetting industry Lorem Ipsum"
    , "industry`s standard dummy text ever since the 1500s,"
    ,"type specimen book, It has survived"
    ,"but also the leap into electronic typesetting, remaining essentially unchanged."
    ,"containing Lorem Ipsum passages and"
    ,"PageMaker including versions of Lorem Ipsum."
];

$phrases = array_map(function($phrase) {
    return preg_replace('/\s+/', '\s+', '/(' . preg_quote($phrase, '/') . ')/iu');
}, array_reverse($phrases));

echo  $model = preg_replace($phrases, '<span style="color:red">$0</span>', $model);

Рабочий пример :

https://3v4l.org/QD8WY

...