通过使用 2 个关键字搜索从字符串中提取一部分
Extract a part from a string by searching with 2 keywords
$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.";
假设我想从 $string
中找到 'typesetting industry.'
和 '1960s'
,如果可用的话 words/sentence 提取这些词之间的其余文本并保存到一个变量中.
所以提取的文本将是
$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 ";
Note that word count doesn't work in my case
Assumed 'typesetting industry.' and '1960s' are unique and must have
in my string.
如何查找和提取这样的内容?
您应该使用一个简单的 (.*)
(捕获所有内容)正则表达式并用您的搜索字符串包围 /$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];
$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.";
假设我想从 $string
中找到 'typesetting industry.'
和 '1960s'
,如果可用的话 words/sentence 提取这些词之间的其余文本并保存到一个变量中.
所以提取的文本将是
$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 ";
Note that word count doesn't work in my case
Assumed 'typesetting industry.' and '1960s' are unique and must have in my string.
如何查找和提取这样的内容?
您应该使用一个简单的 (.*)
(捕获所有内容)正则表达式并用您的搜索字符串包围 /$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];