我有以下正则表达式:
preg_match_all("/.{0,40}<b>(.+?)<\/b>.{0,40}/i", $string, $matches);
目前实际上,它在粗体之前抓取40个字符,在粗体之后抓取40个字符。我该怎么做才能将它从字符改为单词。我怎么能得到它是这样它在粗体之前得到20个单词而在粗体之后得到20个单词?
以下是字符串的示例摘录:
使用关键字“in”或“&lt; b&gt; tagged&lt; / b&gt;”添加标记。蜘蛛侠这将找到包含
的所有结果
答案 0 :(得分:1)
$str0 = 'Tags are add by using either the keyword "in" or "<b>tagged</b>". Spiderman This will find all results containing';
$len0 = strlen($str0);
preg_match_all('/<b>.+?<\/b>/i', $str0, $matches);
$str1 = $matches[0][0];
$len1 = strlen($str1);
$len2 = strpos($str0, $matches[0][0]);
$str2 = substr($str0, 0, $len2);
$len3 = $len0 - $len1 - $len2;
$str3 = substr($str0, $len1+$len2, $len3);
$arr1 = array_reverse(explode(' ', $str2));
$arr2 = explode(' ', $str3);
$key1 = count($arr1) - 1;
$key2 = count($arr2) - 1;
$i = 0;
$before = array();
while($i < $key1 && count($before) < 20) {
if (preg_match('/\w/', $arr1[$i])) {
$before[] = $arr1[$i];
}
$i++;
}
$i = 0;
$after = array();
while($i < $key2 && count($after) < 20) {
if (preg_match('/\w/', $arr2[$i])) {
$after[] = $arr2[$i];
}
$i++;
}
var_dump($before);
var_dump($after);
虽然它有点长代码,但确实有效:
array:before (size=9)
0 => string 'or' (length=2)
1 => string '"in"' (length=4)
2 => string 'keyword' (length=7)
3 => string 'the' (length=3)
4 => string 'either' (length=6)
5 => string 'using' (length=5)
6 => string 'by' (length=2)
7 => string 'add' (length=3)
8 => string 'are' (length=3)
array:after (size=6)
0 => string 'Spiderman' (length=9)
1 => string 'This' (length=4)
2 => string 'will' (length=4)
3 => string 'find' (length=4)
4 => string 'all' (length=3)
5 => string 'results' (length=7)
答案 1 :(得分:0)
尝试在粗体之前和之后获得20个单词。
/(?:[^\s]+\s){0,20}<b>(.+?)<\/b>(?:[^\s]+\s){0,20}/i