[英]how to find a specific set of letters in a block of letters in php
I need to extract some words in a set of letters , , 我需要从一组字母中提取一些单词,
example 例
"gkshgksguvjvjvvjgvjgvjgvgdkgu STACK ksbegkbeskbgksebgkbb OVERFLOW ghbhjjhvjvjvjvjgvgjvgjvgjvgjvjgvgjvgjvgjvjg STACK ksbbb OVERFLOW vjvvvgjvgvgjvgjvgjvgjvvsdkgfkdgdgfdsgkhsdgdgfdsgffsdhkg" “gkshgksguvjvjvvjgvjgvjgvgdkgu 堆栈 溢出 ksbegkbeskbgksebgkbb STACK ghbhjjhvjvjvjvjgvgjvgjvgjvgjvjgvgjvgjvgjvjg 溢出 ksbbb vjvvvgjvgvgjvgjvgjvgjvvsdkgfkdgdgfdsgkhsdgdgfdsgffsdhkg”
I want to extract " STACK ksbegkbeskbgksebgkbb OVERFLOW " 我要提取“ 堆栈 ksbegkbeskbgksebgkbb 溢出 ”
from the above text, 根据以上文字,
number of letters between STACK and OVERFLOW can be vary. 堆叠和溢出之间的字母数可以变化。
if there are more than one satck s and overflow s in the set of letters , I want to find them all with the letters inbetween those two words , (and add them to an array) 如果一组字母中有多个satck和overflow s,我想找到所有两个字母之间都带有字母的字母(并将它们添加到数组中)
each time , the number of letters in between STACK and OVERFLOW can be changed 每次都可以更改STACK和OVERFLOW之间的字母数
$theBlob = "abcSTACKmmmOVERFLOWxyz...";
preg_match_all('/STACK(.*)OVERFLOW/U', $theBlob, $matches);
assert( $matches[0][0] === 'STACKmmmOVERFLOW' );
assert( $matches[1][0] === 'mmm' );
CORRECTED: Added U
modifier to make .*
non-greedy. 已更正:添加了
U
修饰符以使.*
为非贪婪。
The regular express /STACK(.*)OVERFLOW/U
has the U
modifier to make .*
non-greedy which means it should match the minimal number of characters (if no U
, .*
will be greedy and match as much as it can (up to the last "OVERFLOW"). 正则
/STACK(.*)OVERFLOW/U
具有U
修饰符,以使.*
为非贪婪,这意味着它应与最小字符数匹配(如果没有U
, .*
将为贪婪并尽可能匹配。 (直到最后一个“溢出”)。
$matches
will be an array of arrays. $matches
将是一个数组数组。 $matches[0]
is an array of all the full matches. $matches[0]
是所有完全匹配的数组。 $matches[1]
contains an array of all sub-parts in the first set of parenthesis. $matches[1]
包含第一组括号中所有子部分的数组。
See http://phpfiddle.org/main/code/7cix-4y38 看到http://phpfiddle.org/main/code/7cix-4y38
Note: the U
modifier makes all repeaters non-greedy. 注意:
U
修饰符使所有中继器变为非贪婪的。 If you want to mix greedy and non-greedy, you can do .*?
如果要混合使用贪婪和非贪婪,可以执行
.*?
to make just that one non-greedy. 使那只不贪心。 The regex above would become
/STACK(.*?)OVERFLOW/'
上面的正则表达式将变成
/STACK(.*?)OVERFLOW/'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.