Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在正则表达式匹配后获取上一个和下一个10个单词_Php_Regex - Fatal编程技术网

Php 在正则表达式匹配后获取上一个和下一个10个单词

Php 在正则表达式匹配后获取上一个和下一个10个单词,php,regex,Php,Regex,我正在试着在赛后说出前10个单词和下10个单词 我的PHP正则表达式 (?:\S+\s*){0,5}\S*“your age”\S*(?:\s*\S+){0,10} 我匹配的段落 如果您自输入的出生日期起已满28年,则数字28将显示在该文本框中。在标题为“您的年龄”的文本框旁边,您将看到另一个标题为“您出生于”的文本框。此框将显示您出生日期的详细信息,包括您出生的日期 “你的年龄”10个字前后我想要什么 显示在该文本框中。在标题为“您的年龄”的文本框旁边,您将看到另一个标题为“您出生于”的文本

我正在试着在赛后说出前10个单词和下10个单词

我的PHP正则表达式

(?:\S+\s*){0,5}\S*“your age”\S*(?:\s*\S+){0,10}
我匹配的段落

如果您自输入的出生日期起已满28年,则数字28将显示在该文本框中。在标题为“您的年龄”的文本框旁边,您将看到另一个标题为“您出生于”的文本框。此框将显示您出生日期的详细信息,包括您出生的日期

“你的年龄”10个字前后我想要什么

显示在该文本框中。在标题为“您的年龄”的文本框旁边,您将看到另一个标题为“您出生于”的文本框


这里有一个使用
preg\u replace
的选项:

$input = "if you have completed 28 years since the entered date of birth, the number 28 would be shown in that text box. Beside the text box titled “your age”, you would see another text box titled “You Born At”. This box would show you clear details of your birth date including the day on which you were born";
$match = preg_replace("/^.*?((?:\S+\s+){10}“your age”(?:\S+\s+){10}).*$/", "$1", $input);
echo $match;
这张照片是:

shown in that text box. Beside the text box titled “your age”, you would see another text box titled “You Born 
我使用以下模式匹配整个字符串,同时捕获
your age
术语及其两侧的10个单词:

^.*?((?:\S+\s+){10}“your age”(?:\S+\s+){10}).*$

@TimBiegeleisen谢谢,我还找到了另一个解决方案,实际上我更快了一点

(?:\S+\s){0,10}\S*“your age”\S*(?:\s\S+){0,10}
我的代码

$input = "if you have completed 28 years since the entered date of birth, the number 28 would be shown in that text box. Beside the text box titled “your age”, you would see another text box titled “You Born At”. This box would show you clear details of your birth date including the day on which you were born";
$match = preg_replace("/(?:\S+\s){0,10}\S*“your age”\S*(?:\s\S+){0,10}", $input, $match);
echo $match[0];
这会打印出来

shown in that text box. Beside the text box titled “your age”, you would see another text box titled “You Born At”.

如果这需要精确,例如还需要匹配复合词,如“不会”,则应该使用标记器而不是正则表达式模式来检测单词。