Извлечь часть из строки, выполнив поиск по 2 ключевым словам - PullRequest
0 голосов
/ 30 марта 2020
$string="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.";

Допустим, я хочу найти 'typesetting industry.' и '1960s' из $string и, если доступно, эти слова / предложения извлекают оставшийся текст между этими словами и сохраняют в переменную.

Таким образом, извлеченный текст будет

$extracted_text="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 ";

Обратите внимание, что подсчет слов не работает в моем случае

Предполагаемая «индустрия набора текста». и «1960-е» уникальны и должны быть в моей строке.

Как мне найти и извлечь таким образом?

1 Ответ

0 голосов
/ 30 марта 2020

Вы должны использовать простое регулярное выражение (.*) (захватывать все), окруженное строками поиска /$start(.*)$end/. Поскольку строки поиска могут содержать специальные символы регулярных выражений, вы также должны экранировать их, используя preg_quote.

$start = preg_quote("typesetting industry.");
$end = preg_quote("1960s");

$pattern = "/$start(.*)$end/";
$string = "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.";

preg_match($pattern, $string, $matches);
echo $matches[1];

В качестве альтернативы вы также можете разбить строку, используя ключевые слова

$start = "typesetting industry.";
$end = "1960s";

$string = "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.";
$exploded = explode($end, explode($start, $string)[1]);

echo $exploded[0];
...